Click to See Complete Forum and Search --> : Preventing unwanted characters


NevadaSam
05-07-2006, 02:05 PM
Preventing unwanted characters

Instead of searching and replacing unwanted character in strings using cgi/perl, I would like to prevent some characters from ever being typed in text boxes and textfields. Two characters I would like to stop are "|" (pipe symbol) and the "carriage return". I am not even sure how to search and/or replace a carriage return in a string anyway. This may be a JavaScript function, because what I would really like to do is send an alert to tell the user that it is not allowed. Thanks for any ideas you may have on this.

NevadaSam

Logician
05-07-2006, 04:34 PM
Preventing unwanted characters

Instead of searching and replacing unwanted character in strings using cgi/perl, I would like to prevent some characters from ever being typed in text boxes and textfields. Two characters I would like to stop are "|" (pipe symbol) and the "carriage return". I am not even sure how to search and/or replace a carriage return in a string anyway. This may be a JavaScript function, because what I would really like to do is send an alert to tell the user that it is not allowed. Thanks for any ideas you may have on this.

NevadaSam
If this is a homework question, they'll never believe you wrote it...

<FORM>
<TEXTAREA onkeyup="charSuppress(/[\|\n\r]/ig,this)"></TEXTAREA>
</FORM>

<SCRIPT type='text/javascript'>

function charSuppress(barred,txt)
{
if( barred.test(txt.value) )
{
alert('"|" and carriage returns are not allowed.');
txt.value=txt.value.replace(barred,'');
}
}

</SCRIPT>

NevadaSam
05-07-2006, 05:36 PM
Thank you very much. That gives me something to work with. BTW - not homework. I want users to stop hiting the enter key. and the pipe | bar is my field separator.