Click to See Complete Forum and Search --> : validation question...


haynbrian
04-20-2003, 01:17 PM
I'm trying to validate a form with several text fields. Essentially what I did was create functions for each text field, and then create a master function that checks if each function validates, if not it should return false. With what I have right now, if the first text field is correct but the second one is incorrect, the form will submit anyway. Below is an example of the code that I am using:

<script>

function chkOne() {
if (document.form.field1.length == 0) {
alert("Please enter text")
}
return false
}

function chkTwo() {
if (document.form.field2.length == 0) {
alert("Please enter more text")
}
return false
}


function master(thisform) {

var mstVal = false

mstVal = chkOne(form);
mstVal = chkTwo(form);

return mstVal;

</script>

<form name="form" method="post" action="page.htm" onSubmit="return master(form)">

<input type="text" name="field1"> <br>
<input type="text" name="field2"> <br>
<input type="button" name="button" value="submit">


</form>



------------------


This is the best way I could think of doing this. Of course, I have more text fields that above, and I cut out some tags to make it quicker to read. Any suggestions will be greatly appreciated!

Thanks,

Brian

khalidali63
04-20-2003, 01:26 PM
this clearly means that you return false statements in teh functions are out of scope....look into that...

or may be try this form here to see how it works..

http://68.145.35.86/skills/javascripts/UltimateFormValidator.html

DrDaMour
04-20-2003, 01:36 PM
<script>

function chkOne() {
if (document.form.field1.length == 0) {
alert("Please enter text")
}
return false
}

function chkTwo() {
if (document.form.field2.length == 0) {
alert("Please enter more text")
}
return false
}


function master(thisform) {

var mstVal = false

mstVal = chkOne(form);
mstVal = chkTwo(form);


Right here, chkOne doesn't take any parameters, and form is undefined...Secondly no matter what chkone returns it's immediately lost by the caling of chkTwo, What you should do is

mstVal = mstVal & chkTwo();

return mstVal;

</script>

finally from that code, there is NO situation inwhich you ever return true, so i don't see how that could ever work, in fact they are all returning false no matter what.

haynbrian
04-20-2003, 08:34 PM
Khalid,

Your example was perfect. Thanks for the help. If you don't mind though, I have another question.

I liked the way you did the if statement such that:

if(field.name=="field" && field.value=="");

but how would I be able to check two fields in one shot. For example, if I had a form and only people from Los Angeles, CA would get the validation (in that there would be a text input for city, and another input for state), how would I be able to concatenate that into it?

Any help is very appreciated. Thank you!

Brian

khalidali63
04-20-2003, 09:11 PM
If I understood you correctly,this is how you could do that

suppose you have input fields name "city" and "state"
var frm = document.form1;
if(frm.city.value!="" && frm.state.value!=""){
//not empty so do stuff
}esle{
//one opr the other is empty alert message
}

haynbrian
04-20-2003, 10:23 PM
Khalid,

That worked perfectly. Thank you!!!


Brian