Form Validation Script:
<script language="javascript">
function handler(where){
//check for unusual characters
var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
var wspace = " ";
for (var i = 0; i < where.value.length; i++) {
if (iChars.indexOf(where.value.charAt(i)) != -1) {
return false;
}
if (wspace.indexOf(where.value.charAt(i)) != -1) {
return "nope";
}
}
}
function emailhandler(where){
//check for unusual characters
var iChars = "!#$%^&*()+=-[]\\\';,/{}|\":<>?";
var wspace = " ";
for (var i = 0; i < where.value.length; i++) {
if (iChars.indexOf(where.value.charAt(i)) != -1) {
return false;
}
if (wspace.indexOf(where.value.charAt(i)) != -1) {
return "nope";
}
}
}
function isNumberKey(evt,where)
{
var charCode = (evt.which) ? evt.which : event.keyCode;
if (charCode != 8){
var count = where.value.length;
if (count === 0){
where.value = "(";
}
if (count == 4){
where.value = where.value + ")";
}
if (count == 8){
where.value = where.value + "-";
}
if (charCode > 31 && (charCode < 48 || charCode > 57))
{
return false;
}
else
{
return true;
}
}
else
{
return true;
}
}
function checkform()
{
var mycount = document.getElementsByTagName('input').length;
var good = true;
for (i=0;i<=mycount;i++)
{
//alert(document.getElementsByTagName('input')[i].id);
var current = document.getElementsByTagName('input')[i];
current.style.border='1px solid';
//check email
if (current.id == "email"){
if (current.value !== ""){
var test = emailhandler(current);
if(test=="nope"){
alert("Your Email Address can not contain spaces!");
//good = false;
}
if(test===false){
alert("Your Email Address can not contain special characters!");
//good = false;
}
}
}
else
{
if(current.id !== "click"){
if (current.value !== ""){
var newtest = handler(current);
if (newtest===false){
current.style.border='3px solid red';
alert("You have special characters in the field highlighted \r\n" + current.id);
good = false;
}
}
}
}
}
}
return good;
}
function submitform(){
var check = checkform();
alert(check);
}
</script>
If there is an issue in the form I do get the warnings, but The Alert in submitform() is never triggered.
I'm not sure where I'm buggered. Any help is appreciated. If you can streamline the JS that wouldn't be rejected either.