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
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 (("%$#@!*&^/\\".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 = "";
//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="return validate()">
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
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.