Results 1 to 3 of 3

Thread: HTML5 required pattern - cant figure it out

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2011
    Posts
    88

    HTML5 required pattern - cant figure it out

    Hey people.. Im creating a form where one of the text input fields needs to hold an address.. For this i need to make sure that the user types at least letters and numbers.. That is all.. I just cant seem to find out how do to this with JavaScript Regular Expression ..

    What i have is this

    Code:
    <input type="text" size="47" required="true"  name="adresse" required pattern="[A-Za-z ]+\d+[A-Za-z \,\.]+\d+[A-Za-z \,\.]+"/>
    The above is not good enough .. It "works" but only for one type of address like this:

    Sunset road 4C, 1.tv.

    Yes thats how an address could look here in Denmark

    But i also need it to be able to accept an address like this:

    Sunset road 4
    or
    Sunset road 4-C 1-tv.
    or
    Sunset road 4 C 1.t-v.

    And the list goes on .. The easiest way for this i believe is to just make the regular expression check if there is numbers and letters in the field and thats it .. How do i go about that ?

  2. #2
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    Re: HTML5 required pattern - cant figure it out

    I'm guessing that validating an address could get quite tricky, so why not just have it as a required field so they are forced to enter "any" value to submit the form?

    You're using the required attribute incorrectly. The HTML5 Spec says that "true" and "false" are not valid values in attributes. You would want to use:

    HTML Code:
    1. <input type="text" name="address" required />
    or alternatively
    HTML Code:
    1. <input type="text" name="address" required="required" />

    I also noticed you are doing length validation on the input field (size="47"). What if someone has an address longer than 47 characters? It might be extremely unlikely, but still possible? If you are going to do client-side (browser) input validation, don't forget to also do it on the server-side as you should never explicitly trust that what the client is sending to the server is valid data
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2011
    Posts
    88

    Re: HTML5 required pattern - cant figure it out

    Quote Originally Posted by tr333 View Post
    I'm guessing that validating an address could get quite tricky, so why not just have it as a required field so they are forced to enter "any" value to submit the form?

    You're using the required attribute incorrectly. The HTML5 Spec says that "true" and "false" are not valid values in attributes. You would want to use:

    HTML Code:
    1. <input type="text" name="address" required />
    or alternatively
    HTML Code:
    1. <input type="text" name="address" required="required" />

    I also noticed you are doing length validation on the input field (size="47"). What if someone has an address longer than 47 characters? It might be extremely unlikely, but still possible? If you are going to do client-side (browser) input validation, don't forget to also do it on the server-side as you should never explicitly trust that what the client is sending to the server is valid data
    It might end up with just requiring "anything" to be written in the field instead. As for the me using it wrongly thanks for pointing it out eventhough it works the way i have done it ..

    Oh and the size="47" is not length validation its simply the size of the textbox

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width