Quote:
Originally Posted by rwedge
I think the example glenngv posted is a good approach. Reuseable, it could get rid of unwanted spaces and will catch an empty field.
|
Yes, but a single character, even a ? or :<) will return true. My version disregards spaces and will catch a field which is empty or of 'inadequate' length. You could of course strip any spaces as well.
I still prefer to set a minimum (or possibly maximum) length for the input depending on what is sensible or expected under the circumstances. A student's grades should accept only letters A to E as the first character, then only + and - three characters max (not min).
if (/^([A-E][\-\+]?[\-\+]?)$/i.test (grades.value)) {
An address might reasonably require 6 characters minimum. A personal name may not contain numerals (I think!). In short, the precise form of validation ought to be sensitive to the expected answer.