Click to See Complete Forum and Search --> : Validation Help


Nacirema
12-04-2006, 10:14 AM
How would I construct a script for this:

If field1 is null or zero
then
field2 is mandatory

how would that work?

mjdamato
12-04-2006, 10:42 AM
Then what you are really saying is that foeld1 or field2 are mandatory. Kind of differenet than how you stated it. Anyway, this should give you the general idea:

if (!document.formname.field1 && !document.formname.field2) {
alert("You must enter a value in field 2.");
return false;
} else {
return true;
}

yellabuff
12-04-2006, 10:42 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
<script>
function validate(frm) {
if (IsEmpty(frm.field1)){
alert("This field is required");
return false;
}
}

function IsEmpty(txtField) {
if ((txtField.value.length==0) || (txtField.value==null)) { return true; }
}
</script>
</head>
<body>
<form name="form1" action="" onsubmit="return validate(this)">
<input name="field1" type="text">
<input type="submit">
</form>
</body>
</html>

Nacirema
12-06-2006, 12:14 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
<script>
function validate(frm) {
if (IsEmpty(frm.field1)){
alert("This field is required");
return false;
}
}

function IsEmpty(txtField) {
if ((txtField.value.length==0) || (txtField.value==null)) { return true; }
}
</script>
</head>
<body>
<form name="form1" action="" onsubmit="return validate(this)">
<input name="field1" type="text">
<input type="submit">
</form>
</body>
</html>

Thanks
but this doesn't take field2 into account
txtField is field1 I imagine right?
the major actor here is really field 2 though.

yellabuff
12-06-2006, 01:35 PM
<html>
<head>
<title>Untitled</title>
<script>
function validate(frm) {
if (!IsEmpty(frm.field1) && IsEmpty(frm.field2) ){
alert("Field 2 is required");
return false;
}
}

function IsEmpty(txtField) {
if ((txtField.value.length==0) || (txtField.value==null)) { return true; }
}
</script>
</head>
<body>
<form name="form1" action="" onsubmit="return validate(this)">
Field 1:<input name="field1" type="text">
Field 2:<input name="field2" type="text">
<input type="submit">
</form>
</body>
</html>