
Originally Posted by
Ralze34
I am looking to do something similar to this. However, I was hoping to perform the validation and field highlight dynamically and not use the submit. So when the user fills out the information and clicks on the next form field or tabs over it checks the info and if it is wrong changes the color.
JS:
Code:
validate(field, responseid){
var vfield = document.getElementById(field);
var res = document.getElementById(responseid);
if(vfield.value > 99 || vfield.value < 12){
vfield.style.backgroundColor='red';
res.innerHTML = "This is not a valid age!";
}
}
Form:
Code:
<input id="age" type="text" onblur="javascript:validate('age','agetxt');"><div id='agetxt'></div>
Apply the validation script to check the value of textbox1, then upon changing to the next field it'll run. This will highlight the textbox itself. Then, in the div (agetxt) it will give them a response and inform the user what they did wrong. You can also take it a step further and make the JS script re-focus on the textbox being validated. Of course, in the JS script, unless you want to make a function for each field you'll probably want to make another parameter. The new parameter ideally would determine what type of value to compare with (age, email, address, name, etc.).
This is completely untested and might not work as is, but it should give the idea.
Bookmarks