The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Web Design
> HTML Programming
|
Limit what can be entered on text field.
Discuss Limit what can be entered on text field. in the HTML Programming forum on Dev Shed. Limit what can be entered on text field. HTML Programming forum covering discussions of HTML and XHTML, as well as HTML-related issues such as writing W3C Compliant code. Use HyperText Markup Language for building websites.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 30th, 2013, 08:52 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 10
Time spent in forums: 4 h 41 m 31 sec
Reputation Power: 0
|
|
|
Limit what can be entered on text field.
Hello, i have a input form on my site.
Code:
<form method="post" action="Untitled-2.php">
<input name="search" style="font-size:110px" type="text" class="searchBox" size="700" maxlength="6">
</form>
I managed to limit total length to 6 chars, but now i want to go deeper.
I want to let users to enter chars only in this format
"111AAA" that means 3 letters and 3 numbers and only this way , not AAA111 or 1A1A1A, only 111AAA
How can i achive this? Maybe javascript?
Best Regards
Mix
|

January 30th, 2013, 10:03 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
|
Hi,
if you actually want to enforce those restrictions, you need to validate the input on your server. HTML attributes or JavaScript can only be used to inform the user about errors and make the form more convenient.
|

January 30th, 2013, 02:04 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 10
Time spent in forums: 4 h 41 m 31 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by Jacques1 Hi,
if you actually want to enforce those restrictions, you need to validate the input on your server. HTML attributes or JavaScript can only be used to inform the user about errors and make the form more convenient. |
What method is best to achive this? Maybe somehow check with php before form submiting?
I´m submiting my form like this:
PHP Code:
if (isset($_POST)) {
if (isset($_POST["search"])){
$a = $_POST['search'];
echo $a;
//do something more
|

January 30th, 2013, 02:13 PM
|
 |
Contributing User
|
|
Join Date: Sep 2012
Posts: 148
Time spent in forums: 18 h 39 m 9 sec
Reputation Power: 1
|
|
|
You can also use jquery or javascript with a regular expression ( preg_match ) to check client side. Regular expressions are used to check for patterns and can check forvthe most complex patterns.
|

January 30th, 2013, 05:26 PM
|
 |
Code Monkey V. 0.9
|
|
Join Date: Mar 2005
Location: A Land Down Under
|
|
Checking string length with PHP is simple.
PHP Code:
$length = strlen ($_POST['search']);
Then you can check that the length is between whatever you need it to be.
|

January 30th, 2013, 06:07 PM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
Well, since you want a certain pattern, you can check that with a regular expression (like notflip already said):
The regular expression for a string of "three letters (a to z, A to Z) followed by three decimal digits" is
So the PHP check would be
PHP Code:
if ( preg_match('/^[a-z]{3}\\d{3}$/i', $_POST['search']) )
// input matches
else
// input doesn't match
Note that you don't need an explicit length check in this case, because it's implied by the pattern.
|

May 7th, 2013, 04:07 AM
|
|
Permanently Banned
|
|
Join Date: Apr 2013
Location: Ludhiana
Posts: 33
Time spent in forums: 11 h 47 m 53 sec
Warnings Level: 15
Number of bans: 1
Reputation Power: 0
|
|
|
you will need to use validations in your command as mentioned above by Jacques1.
|

May 7th, 2013, 10:16 AM
|
|
Registered User
|
|
Join Date: Apr 2013
Posts: 11
Time spent in forums: 1 h 33 m 53 sec
Reputation Power: 0
|
|
|
Server side validation is the way to go. Use Jaques1 example.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|