OK, I'm going to make this short and sweet:
Problem: You are allowing users to enter information into a form; you want to make sure that their strings contain only a certain subset of characters.
Solution: Know what characters you want to include; step through their form input one character at a time and test to see if the current character is in the set of inclusive characters. If it is not, then flag that the string is invalid.
What we need to do is code the
ONSUBMIT event in the
FORM tag. So, let's say
that we have a FORM with an INPUT box where someone enters their UserName. We want to make sure that
the UserName contains only characters A-Z, a-z, 0-9, _ (underscore), or - (dash). Here's how we'd do
(The above code is available at the end of this article...)
validation, you save a round-trip back to the server since the browser makes sure the inputs are formatted properly
before submitting the form.
To see the alert box the user will see if he or she enters an invalid UserName, simply click the button