It's so easy to make subtle, impossible to spot mistakes with your regular expression.
If you can't use expression forces Java Script to do type coercion on your input value; it must first be interpreted as a number for the subtraction operation.
The check on the length is for a special case involving empty strings.
Also note that it falls down on your 0x89f test, but that's because in many environments that's an okay way to define a number literal.
This tutorial will show you how to create a Java Script-enabled form that checks whether a user has filled in the form correctly before it's sent to the server. First we'll explain why form validation is a useful thing, and then build up a simple example form, explaining things as we go along.
At the end, there's a little exercise to keep you busy too!
There are two main methods for validating forms: (usually done using Java Script).
Server-side validation is more secure but often more tricky to code, whereas client-side (Java Script) validation is easier to do and quicker too (the browser doesn't have to connect to the server to validate the form, so the user finds out instantly if they've missed out that required field! In this tutorial we'll build a simple form with client-side Java Script validation.
Since the left hand side is now numeric, type coercion is again used.
Shows you how to write a script that ensures your form is filled in correctly before it's sent to your server.
Great for "required fields" such as email addresses!
If you want to catch that specific scenario you could add an additional check. Simply trimming the string before the length check will handle this case.
Now you can ask questions directly to me on my new site ASPForums.