View Full Version : Data validation

11-19-2011, 08:42 AM
I am new to html programming so i am just wondering if this is at all possible to do in HTML i know in Visual Basic programming you could validate the data the user enters in text fields like IsNumeric etc. to make sure the user only entered in numeric for certain fields. I want to know if there is a way i can do this in HTML

I have seen some example of people using JavaScript to do this type of data validation but i am wondering if there is a way to do it without the help of javascript ???

11-19-2011, 08:49 AM
Also Is there a way to do something like this: lets say a zip code text field would only need to be 5 characters long (numeric) FOR the United States e.g 71458

can a text field be fixed to only allow the user to enter in 5 numeric characters only?

11-19-2011, 02:14 PM
Let me answer your second question first: To limit the amount of characters that can be written in a text input you set the maxlength attribute.

<input type="text" maxlength="25">

The above limits the character count to 25. And with the size attribute you can influence the basic width of the input. I. e. a size="5" would make the input wide enough to show five characters at once.

Now, your first question is a little more elaborate. The short answer is: it’s not possible with HTML alone. You need to use a server side script to validate the form input and can enhance user friendliness with JavaScript validation. The long answer is: It may be partly possible in the future version of HTML (that is HTML 5). As far as I can see there is the pattern attribute (http://dev.w3.org/html5/spec/Overview.html#attr-input-pattern) which can be set to a regular expression against which the browser will validate the input.

But here comes the big however: Besides probably not being supported in a lot of browsers for quite a while yet this is still client-side validation only and is unreliable at best. For example, using Firebug or any similar debugging tool in other browsers I can just remove attributes or change attribute values on the fly and then submit the form with “wrong” data. You still need to do server side validation at all times if you want to make sure. JavaScript today and HTML 5 in future won’t save you from doing that.