Thursday, June 21, 2012

JavaScript - How to keep original value after validation failure

Most of us I hope are familiar with JavaScript validation methods. For example, you have two fields: my_field_01 and my_field_02, both are mandatory, you populated one field with some value, left the other blank and submitted the form, the required field validator ( if my_field_01.value = "" alert("This is a mandatory field")  ) will display a message, both fields will go blank. It's not so cool when you have twelve fields which are being reset every time you did something that caused an alert message to pop up.
The solution is quite simple: add one line in your statement, possibly right after the alert message line.

if (form == "OK") {
   document.form.submit();
}
else {
   alert("You have errors”);
   event.returnValue = false;}

Cheers!

No comments:

Post a Comment