How to validate all fields with the same function in JavaScript?

FrUingirott

New Member
I need to do the following, but I couldn't find any example of similar form validation on the web:\[code\] <script type="text/javascript"> function something(){ if the value on the field who is calling this function == 2,4,6 or 8 alert("This number is invalid") focus in this field. </script>Field1 call something()Field2 call something()Field3 call something()Field4 call something()Field5 call something()Field6 call something()\[/code\]I've tried like this:\[code\]function validate() { valid = true; if ( document.form.field_name.value =http://stackoverflow.com/questions/3752139/="2" || document.form.field_name.value =http://stackoverflow.com/questions/3752139/="4" || document.form.field_name.value =http://stackoverflow.com/questions/3752139/="6" || document.form.field_name.value =http://stackoverflow.com/questions/3752139/="8" ){ alert ( "Invalid number." ); valid = false; document.form.field_name.focus(); } return valid; }\[/code\]I'm calling the function like this: \[code\]a)"Some text" <input type="text" name="aa" size="1" maxlength="1" onkeypress="return validate(aa)"><br>\[/code\]But this way I would have to create a different function for every field. So how could I implement this?
 
Back
Top