Click to See Complete Forum and Search --> : One form field must be filled out


mhumble
01-29-2004, 10:09 AM
I have a form with 4 form fields, I need validation that at least one form field must be filled out before submittion.

Thanks

nmcm
01-29-2004, 10:30 AM
It would help YOU out if some code was here. Still

Forms have the event onsubmit. You ca use a function to check the values of the form fields, if at least one the values is diferent from '' the function will return true else false. Here is an example

<script>
String.prototype.trim=function(){
return this.replace( /^\s+/, "" ).replace( /\s+$/, "" )
}

function submitOk(){
// removing all with spaces before string and after string
_f1 = document.yourform.f1.value.trim()
_f2 = document.yourform.f2.value.trim()
_f3 = document.yourform.f3.value.trim()
_f4 = document.yourform.f4.value.trim()
if ( _f1 != "" || _f2 != "" || _f3 != "" || _f4 != "" )
return true
return false
}
</scritp>

<form name=yourform onsubmit="return submitOk()">
<input type=text name=f1 value="">
<input type=text name=f2 value="">
<input type=text name=f3 value="">
<input type=text name=f4 value="">
<input type=submit value="Go on dude">
</form>

When you click the submit button submitOk is invocked, and if one of te fields has a value the form is sumited, if all fileds are empty it will not.