Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking a Text Field for Special Characters 1

Status
Not open for further replies.

Kafenated

Programmer
May 8, 2003
11
IN
I'm new at javascript. I need a validation to check a value of a text field. If field contains a comma, single or double quote then send message. I know how to send the message my question is the script to check for characters

Thanks in Advance
 
Your validate function could look something like this:

function validate()
{
//the id would obviously have to refer to the id you've given to the text box whose value you wish to check
var s = document.getElementById('txtBoxToCheck').value;

//traverse the text string and look at each individual character
for (var i=0; i < s.length; i++)
{

var keychar = s.charAt(i);

//put whatever characters you do not want to appear
//in the string surrounded by double quotes
if ((&quot;%$#@!*&^/\\&quot;).indexOf(keychar) > -1)
{
//tell user the character is illegal
alert(keychar + ' is not allowed');

//clear out the text box so the user can start over
document.getElementById('txtBoxToCheck').value = &quot;&quot;;

//this cancels submission of the form
return false;
}
}

//if we've made it this far there were no special characters
return true;
}

The call to the function would look something like this:
<form onsubmit=&quot;return validate()&quot;>

If you would like to prevent special (or any other)characters as the user is typing, you can refer to my answer in this thread:
thread215-549191
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top