PDA

View Full Version : how to validate the field


petertran123
09-13-2002, 04:01 PM
I previous posted, but i can't get a straight answer. Please Help.....

i create 2 forms:

form 1: with has PrimarySSN and SecondarySSN after user fill in and click submit and process to the second page which is

form 2: on form 2 will display previous information which is PrimarySSN and SecondarySSN.

on form 2 there are more fields need to fill in, example name and address.

form here i wanted to validate the SecondarySSN, the form will check if SecondarySSN has pass parameter to the form then require the user to fill in First name and Last Name. On otherhands, If SecondarySSN field parameter did not pass from form1 then the field empty. If the secondaySSN field is empty then do not need to process or require to fill out First name and Last Name. the SecondarySSN is padding and let the form submit without it..

Please help me as your wiset

Peter

whammy
09-14-2002, 07:23 AM
Wow... that sounds like you took something simple and made it complicated.

That should be easy... just write your code using what you JUST SAID in english... but instead of english, use if/then statement(s)...

petertran123
09-16-2002, 05:55 PM
I'm using this code, but it seems not to work as i wanted. Would you please take a look. In this code is validating with or without value from SecondarySSN field. I wanted the first name and last field not required when SecondarySSN is empty.

function isMultiValidate(){
if(document.form.SecondarySSN.value != ""){
errMsg = "The following fields are required \n"
showErr = false;

if (document.form.SecondaryNameFirst.value == ""){
errMsg += "First Name \n";
showErr = true;
}
if (document.form.SecondaryNameLast.value == ""){
errMsg += "Last Name \n";
showErr = true;
}
}
if (showErr){
alert(errMsg);
return false;
}
else{
return true;
}
}




</script>

whammy
09-16-2002, 11:58 PM
This should be easy... just set errMsg to "" to start with... and then do this:

function isMultiValidate(){
if(document.form.SecondarySSN.value != ""){
if (document.form.SecondaryNameFirst.value == ""){
errMsg += "First Name \n";
}
if (document.form.SecondaryNameLast.value == ""){
errMsg += "Last Name \n";
}
}
}

Then check to see if errMsg is still "" ... if not, alert the errMsg and return false; else return true. If you do it like that you don't even need the other variable.

petertran123
09-17-2002, 03:48 PM
Whammy,

I use your code, but it seems nothing happen. Please read the following code below. In this scenerio, i'm still not able to submit the form, if SpouseSSN blank. the message still pop up and asking for first and last name. even SpouseSSN field are empty. i stucked right here.

First i want the form look for the SpouseSSN field, if the SpouseSSN blank then do not required pop up msg asking for first and last name of Spouse.

Second both first and last name of spouse REQUIRED only if SpouseSSN NOT BLANK. please take a look at the code below and help me to solve problem above.


*************
function isValidate(){
errMsg = "The following fields are required \n"
showErr = false;
if(document.form.SpouseSSN.value != ""){
if (document.form.SpouseNameFirst.value == ""){
errMsg += "First Name \n";
showErr = true;
}
if (document.form.SpouseNameLast.value == ""){
errMsg += "Last Name \n";
showErr = true;
}
}
if (showErr){
alert(errMsg);
return false;
}
else{
return true;
}
}

***********************

whammy
09-17-2002, 04:55 PM
This works (I also included an example of SSN validation):


<script language="JavaScript" type="text/javascript">
<!--
function isValidate(){
var errMsg = "The following fields are required: \n"
var showErr = false;
var ssnre = /^(\d{3})[-\s\.]?(\d{2})[-\s\.]?(\d{4})$/
var f = document.form1
if(f.SpouseSSN.value != ""){
if(f.SpouseNameFirst.value == ""){
errMsg += "First Name \n";
showErr = true;
}
if(f.SpouseNameLast.value == ""){
errMsg += "Last Name \n";
showErr = true;
}
if(!ssnre.test(f.SpouseSSN.value)){
errMsg += "Invalid Social Security Number \n";
showErr = true;
}
else{
f.SpouseSSN.value = f.SpouseSSN.value.replace(ssnre,"$1-$2-$3");
}
}
if(showErr == true){
alert(errMsg);
return false;
}
else{
return true;
}
}
// -->
</script>

<form name="form1" onsubmit="return isValidate()">
SpouseSSN: <input type="text" name="SpouseSSN" /><br />
SpouseNameFirst: <input type="text" name="SpouseNameFirst" /><br />
SpouseNameLast: <input type="text" name="SpouseNameLast" /><br />
<input type="submit" />

petertran123
09-17-2002, 06:45 PM
Still did not work Whammy,

i tried to submit the form with out SpouseSSN value, and still receiving popup msg:

The following fields required:
last name
first name
Invalid SSN

i do not want it happen. if SpouseSSN is blank then do not popup msg or do not required spouse fill out his/her name.

In addition, if Spouse fills out his/her SSN, and forget to enter their first and last name..from here they should receive msg said " please enter your first and last name"...do you know what i mean?..Thank you for your patient.

whammy
09-18-2002, 03:57 AM
Did you copy the code above exactly? It worked for me...

Did you name your form?

In the example I posted I named the form "form1" - so if it's named differently you'd need to change the line

var f = document.form1

to reflect the name of your form.

petertran123
09-18-2002, 02:52 PM
Yes i do copy exactly the same..the only thing i changed is form1 to form

and i also change to document.form

i don't know why it not work on mine..i test over 10 times and still receiving the same thing....the problem i see that the SpouseSSN blank and the other the 2 fields (lastname and firstname) still required, but i don't want that's happened. If the SpouseSSN blank, msg do not need to popup and do not need to validate or required. In this way, user can submit the form without SpouseSSN and other info.

whammy
09-18-2002, 10:55 PM
form is a reserved word in javascript. Change that to something else (like form1 - as in my example), and it will work.