PDA

View Full Version : commas in form fields


andyc209
01-29-2004, 10:30 AM
is there anyway I can stop someone using commas and ENTER within a text area field via form validation?

Kor
01-29-2004, 11:01 AM
commas, yes
Enter, no.

I mean you can not change the default meaning of some special keys (F5, Enter, Alt+F4, Esc and some others) because those are the browser's or OS's attributes, not the web page's.

Well, If Enter will submit, you can prevent the submit action, but you can not prevent the ENTER action, if you know what I mean...

andyc209
01-29-2004, 11:02 AM
can you point me to a script with comma validation?

Kor
01-29-2004, 11:45 AM
Try this, (not the brightest ideea but this is all that cames in my mind now ):cool:


<html>
<head>
<script>
function filter(field) {
var valo = new String();
var char = '0123456789.qwertyuiop[]asdfghjkl;\'zxcvbnm/QWERTYUIOP{}ASDFGHJKL:"ZXCVBNM<>?/|!@#$%^&*()_+-=~`';
var chars = field.value.split("");
for (i = 0; i < chars.length; i++) {
if (char.indexOf(chars[i]) != -1) valo += chars[i];
}
if (field.value != valo) field.value = valo;
}
</script>
</head>

<body>
<textarea name="text" cols="" rows="" onkeyup="filter(this)"></textarea>
</body>
</html>

Vladdy
01-29-2004, 11:54 AM
Ouch....

Kor, no offense, but you need to read about regular expressions and start using them.

if(/,/.test(document.getElementById('youTextArea').value))
{ /*commas found */
}
else
{ /*no commas */
}

Kor
01-29-2004, 12:04 PM
"Right Said Fred"... :D I know, I know... I promised I'll do so, Sir

Yet, but my stupid code DID stop the ENTER also :D .

glenngv
01-30-2004, 07:25 AM
If you don't want users to put enter into the field, then why did you use textarea in the first place? Use single-line text field instead.

<input type="text">

Or did you mean just input text field when you said "text area field"?

Danne
01-30-2004, 12:29 PM
Maybe you find this discussion useful:
http://www.codingforums.com/showthread.php?s=&threadid=31114