PDA

View Full Version : Form Validation


pthompson2002
09-27-2002, 03:28 PM
I'm using the following line of JavaScript to define the legal characters in a textarea.

charpos = objValue.value.search("[^A-Za-z0-9 :;/?!&@$£€#%*=()<>_+,.-]");

I would like to include a 'carriage return' in this list as at the moment if a user trys to submit a message including carriage returns it rejects it. Can anyone tell me how I can do this?

BrainJar
09-27-2002, 04:09 PM
This is from the JS 1.5 reference at http://developer.netscape.com/docs/manuals/js/core/jsref15/regexp.html#1193136:


\f - Matches a form-feed.
\n - Matches a linefeed.
\r - Matches a carriage return.
\t - Matches a tab.
\v - Matches a vertical tab.

\s - Matches a single white space character, including space, tab, form feed, line feed. Equivalent to [ \f\n\r\t\u00A0\u2028\u2029].


Some systems use \n while others use \r\n so you would want to include both. Or use \s to allow all white space.

pthompson2002
09-27-2002, 04:20 PM
I'm having trouble getting the valid string to except \n, do you know how I can get the validator to read it as "\n". I have tried it with "+\n+" and '\n' and \n and it doesn't work.

adios
09-27-2002, 04:24 PM
Try:

\\n

(double escape)

beetle
09-27-2002, 06:17 PM
Ya, what adios said, because you are defining your pattern as a string, and not as a pattern, so your escaping backslash for the \n needs to be escaped from the string itself.charpos = objValue.value.search("[^A-Za-z0-9 :;/?!&@$£€#%*=()<>_+,.-\\n]"); Where strings use the ' or " as a delimeter, patterns use the foward slash / So, if you want to define this as a pattern instead, do thischarpos = objValue.value.search(/[^A-Za-z0-9 :;/?!&@$£€#%*=()<>_+,.-\n]/);Now the extra escaping backslash is unecessary. I would consider this to be the preferred method, although you have several meta-characters in there that need to be escaped as wellcharpos = objValue.value.search(/[^A-Za-z0-9 :;\/\?!&@\$£€#%\*=\(\)<>_\+,\.-\n]/);For reference, here is a list of metacharacters which need to be escaped to generate a string match/ \ | () [ { ^ $ * + ? .Also, some characters in specific instances will need to be escaped as well. Consider this patterm/[0-9]/Will match any digit between and including 0 and 9. However, THIS pattern /[0\-9]/ Will match only a 0, a 9, or a dash (-)

Pooh
09-27-2002, 07:05 PM
I believe you don't need the escape for the special characters inside the brackets [] of the character class but don't quote me on that.

beetle
09-27-2002, 07:11 PM
Originally posted by Pooh
I believe you don't need the escape for the special characters inside the brackets [] of the character class but don't quote me on that. Uh, didn't my example just show that you DID have to? (in certain cases)

:confused:

pthompson2002
09-30-2002, 10:13 AM
Thanks for the help. I used the following approach but on submiting it ignored the illegal characters I intentionally added.

charpos = objValue.value.search(/[^A-Za-z0-9 :;\/\?!&@\$£€#%\*=\(\)<>_\+,\.-\n]/);

However when i removed the "\n" the validator picked out the illegal characters. I have tried the other combinations you suggested but none of them seem to allow for carriage returns AND check for illegal characters.

For example if I had "\" in my text it should highlight it as an illegal character, without the "\n" it does but when it is included it ignores it.

Garadon
09-30-2002, 10:19 AM
tried this?
charpos = objValue.value.search(/[^A-Za-z0-9 :;\/\?!&@\$£€#%\*=\(\)<>_\+,\.-\\n]/);

pthompson2002
09-30-2002, 10:29 AM
I have tried that and it also does not work

mordred
09-30-2002, 12:34 PM
Well, BrainJar has given you already the best hint what's wrong in your code. So I can only expand on what he said, because I noticed two things that presumably screw up your RegExp:


[^A-Za-z0-9 :;\/\?!&@\$£€#%\*=\(\)<>_\+,\.-\n]


That threw instantly an error at me in IE5.5, complaining about an illegal range - which is understandable, because an unescaped hyphen between two characters stands for a character range, but a range can't go from . to \n.

The other issue is connected with the platform you operate on. A linefeed in a <textarea> on windows consists of \r\n (or vice versa, can't remember exactly), so you also have to include the \r in your legal characters.

Let's try to run


[^A-Za-z0-9 :;\/\?!&@\$£€#%\*=\(\)<>_\+,\.\-\r\n]


and report how that worked for you.

Pooh
09-30-2002, 01:02 PM
No escapes needed within the [] as long as you're careful

<script>
myRegExp = /[^\w\s:;/?!&@$£€#%*=()<>+,.-]/ig
myString="some crap you want to check:;/?!&@$£€#%*=()<>+,.-34545712340 ]] }}}"
matchArray=myString.match(myRegExp)
alert("These characters are not allowed\n" +matchArray)
</script>