|
-
Mar 12th, 2013, 09:26 AM
#1
Thread Starter
Lively Member
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 ?
-
Mar 12th, 2013, 06:08 PM
#2
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:
<input type="text" name="address" required />
or alternatively
HTML Code:
<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
-
Mar 13th, 2013, 01:40 AM
#3
Thread Starter
Lively Member
Re: HTML5 required pattern - cant figure it out
 Originally Posted by tr333
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:
<input type="text" name="address" required />
or alternatively
HTML Code:
<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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|