justineaguas Posted June 19, 2010 Share Posted June 19, 2010 Hello, I want to know how to validate multiple textboxes in one form. I just studied JavaScript yesterday and I tried this code from www.w3schools.com that validates one textbox in a form. function validate_required(field,alerttxt) { with (field) { if (value==null||value=="") { alert(alerttxt);return false; } else { return true; } } } function validate_form(thisform) { with (thisform) { if (validate_required(txtProductName, "Fill it out!")==false) {return false;} } I tried adding another if statement after the code to validate other textboxes other than txtProductName. Sadly, it didn't work. Can anyone help me in validating multiple textboxes in one form only and it only shows one alert message. Please please please. Thanks for the future help! Quote Link to comment Share on other sites More sharing options...
theverychap Posted June 19, 2010 Share Posted June 19, 2010 // set error to false var error = false; // get all the textareas... var textAreas = document.getElementsByTagName('textarea'); // loop through the textareas, checking the value is not empty... for ( i=0; i <= textAreas.length; i++ ) { // if any one of them is empty, error will be set to true... if ( textAreas[i].value == '' ) { error = true; } } // is there an error? (is error true) if ( error ) { alert('there are errors...'); } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.