Validating filenames with regularexpression validator Chatterbot

Java Script restricts the first character to letters, $, or an underscore.

The pattern, /^[a-z$_] specifies the set of characters allowed for the first character.

The tests all ask the same question: "Does a minimal amount of data exist anywhere in the string? The set could have been written [a-z A-Z] to include both upper and lower case letters and the i flag would have been unnecessary.

After ensuring there is data, the next basic test is if there is the right amount of data.

The most fundamental test is whether or not the field has data. It uses the character set [a-z] in combination with the case insensitive flag i and the to require three letters together.

Below are three different test to determine if the string contains data. We worked with the results of pressing the enter key in Convert Returns to . It use the metacharacter \w and the to test for one or more character. These can be anywhere in the string and in combined with any other characters just as long as there are three letters together.

If that fails, then the string is tested for a space character with \s (the difference is whether the is upper or lower case).

If that wasn't the cause of failure, the string is tested for too many characters—11 or more with . In the second pattern we are looking for a valid Java Script variable name of ten characters.The first function tests for exactly 10 nonspace characters with \S.There cannot be additonal characters before or after the matched 10 because the expression pattern requires the 10 characters to start at the beginning of the string and go to the end by using ^ and $: /^\S$/.The i flag at the end of the expression makes the pattern case insensitive.The expression could have been written /^[a-z A-Z$_]\w$/ specifying both upper and lower case letters in the character set eliminating the need for the flag.Both examples make use of the metacharacter \b for word boundary.

Tags: , ,