...

View Full Version : Javascript formchecking recognizing error but letting you threw



shortkid422
01-03-2004, 01:33 AM
I have this code and when you make a mistake in the input, it will give you the error message, but when you click OK, then you can go to the next page. Also, if there is no error, then it wont let you submit it. here is the code:



<script Language="JavaScript">

function IsEmpty(aTextField) {

if ((aTextField.value.length==0) ||

(aTextField.value==null)) {

return true;

}

else { return false; }

}

function IsNumeric(sText)

{

var ValidChars = "0123456789";

var IsNumber=true;

var Char;

for (i = 0; i < sText.length && IsNumber == true; i++)

{

Char = sText.charAt(i);

if (ValidChars.indexOf(Char) == -1)

{

IsNumber = false;

}

}

return IsNumber;

}

function ValidateForm(form)

{

if(IsEmpty(form.adult_number || form.children_number))

{

alert('You need to enter the amount of people that are attending this vacation.')

form.account_number.focus();

return false;

}

if(form.adult_number && form.children_number < 1)

{

alert('You need to enter the amount of people that are attending this vacation.')

form.account_number.focus();

return false;

}


if (!IsNumeric(form.adult_number.value || form.children_number.value || form.infant_number.value))

{

alert('Please enter only numbers the fields')

form.account_number.focus();

return false;

}

return false;

}

</script>

josses
01-03-2004, 07:33 PM
when you are calling it in the <form name"whatever" Action="whereEver.htm" onSubmit="return yourValadationFunction()" >

it is key that you have a return false if there is a problem with a blank field

there is also a form validation script in the download scripts area that i made and you can tell the name of the field that is null it is quite simple to use

best of luck

josses
01-03-2004, 07:35 PM
function IsEmpty(aTextField) {

if ((aTextField.value.length==0) ||

(aTextField.value==null)) {

return true;

}

else { return false; }

}


you have the returns in here backwards

should be

function IsEmpty(aTextField) {

if ((aTextField.value.length==0) ||

(aTextField.value==null)) {

return false;

}

else { return true; }

}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum