PDA

View Full Version : Fields are required and validation


petertran123
09-10-2002, 03:21 PM
Hello all helper,

I created, a form and submit into text file: I wanted to be able to validate the field as follow:

1. Fname="FName"
2. Lname="Lname"
3. SSN = SSN

the problem is i wanted to validate both field Fname and Lname when someone is entering SSN, if not they are not enter SSN then the other 2 fields do not need to be validated. When they enter SSN, and forgot to fill out Fname and Lname then a msg popup say something, "Please enter your First and Last Name"

PLease help me as your wiset,
Peter

ShriekForth
09-10-2002, 04:27 PM
The following script will do what you are looking for, You will need to add
onSubmit="return validate(this)"
to your form


<script type="text/javascript" language="JavaScript">
function validate(frm){
errMsg = "The following fields are required \n"
showErr = false;
if(frm.SSN.value != ""){
if (frm.Fname.value == ""){
errMsg += "First Name \n";
showErr = true;
}
if (frm.Lname.value == ""){
errMsg += "Last Name \n";
showErr = true;
}
}
if (showErr){
alert(errMsg);
return false;
}
else{
return true;
}
}
</script>


The important part is the returning of true or false. If it returns false, the form will not be submitted.

ShriekForth

Keltoi
09-10-2002, 05:15 PM
I found a very useful solution to this situation this morning:
http://www.peterbailey.net/fValidate/

petertran123
09-10-2002, 06:35 PM
Thanks for your helping.

I used ShriekForth codes, but it seems to work with validation for all even they are enter SSN or not. I'm talking about, when people are entering SSN and do validate for Fname and LName fields. otherwise, if SSN field is empty and no need to validate Lname and Fname field. Sorry i can't explain well