I have a form that needs to have its fields completed, and check for unwanted characters:
However, with this its allowing all characters through.
I only need these characters to be allowed:
a-z
A-Z
0-9
@#-.,
Anything else should pop up the warning message.
Code:
var reqfields = Array("Email::Email", "Firstname::First Name", "Lastname::Last Name", "month::Birthdate Month", "day::Birthdate Day", "year::Birthdate Year", "Address::Address", "City::City", "State::State", "Zip::Zip");
function checkIt() {
var msg = "The following fields were either left blank or contained special characters, please fix and resubmit. \n\n";
// var str = /^[\w+\d+]/;
var str = /[^a-zA-Z0-9\s@#-\.,]/;
// var str = /[^~!$%^&*()_+|`{}[]:;<>?]/;
for (var i = 0; i<reqfields.length; i++) {
thStuff = reqfields[i].split("::");
thText = thStuff[1];
thName = thStuff[0];
theField = document.g_form[thName];
if (theField.value == "") {
msg += thText+" Is REQUIRED \n";
} else if (g_form[thName]) {
if (str.test(document.g_form[thName].value)) {
msg += thText+" Can Only Have Letters Or Numbers\n";
}
}
}
if (msg.length>255) {
alert(msg);
return false;
}
return true;
}
However, with this its allowing all characters through.
I only need these characters to be allowed:
a-z
A-Z
0-9
@#-.,
Anything else should pop up the warning message.