javascript Inline Validation
function ValidateForm(){ var sMsg = ''; //error message displayed var firstfield = ''; //used for setting focus //repeat for each field to be validated if (document.getElementById("txtField1").value == '') { sMsg = sMsg + 'field1, '; if(firstfield == '') { firstfield = 'txtField1'; } } if (document.getElementById("txtField2").value == '') { sMsg = sMsg + 'field2, '; if(firstfield == '') { firstfield = 'txtField2'; } } if (sMsg != '' ) { sMsg = 'Please enter your ' + sMsg + '\n'; } if (sMsg != '' ) { //alert(sMsg); //used for debugging document.getElementById("divError1").innerText = sMsg; document.getElementById("divError2").innerText = sMsg; if (firstfield != ''){document.getElementById(firstfield).focus();} return false; } else { return true; } }
Validates a form (2 fields within) and instead of alerting the error(s), it displays them within 2 divs, which can be styled appropriately.
Updated: Thursday 7th October 2010, 11:12am
There are 0 comments
Comments are currently closed.