#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    10
    Rep 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
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    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.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    10
    Rep Power
    0
    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?

    Im submiting my form like this:

    PHP Code:
    if (isset($_POST)) {
      if (isset(
    $_POST["search"])){
        
    $a $_POST['search'];
        echo 
    $a;
        
    //do something more 
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    204
    Rep Power
    2
    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.
  8. #5
  9. Code Monkey V. 0.9
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2005
    Location
    A Land Down Under
    Posts
    2,101
    Rep Power
    1990
    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.
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    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
    Code:
    /^[a-z]{3}\d{3}$/i
    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.
  12. #7
  13. No Profile Picture
    Permanently Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Location
    Ludhiana
    Posts
    33
    Rep Power
    0
    you will need to use validations in your command as mentioned above by Jacques1.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    9
    Rep Power
    0
    Server side validation is the way to go. Use Jaques1 example.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Posts
    111
    Rep Power
    16
    What i normaly use is JS on the users end to check dynamicaly on blur. I then have server side checking incase they are malicious or run "no script". This way everything gets checked regardless.

IMN logo majestic logo threadwatch logo seochat tools logo