ifis Posted September 25, 2008 Share Posted September 25, 2008 I found a script to validate a form by getElementsByTagName . I have the validation set up to validate <input>s , but I want to add <textarea>s . I'm not too up on javascript and have tried to do some playing around without any sucess. Here is the code: function validate() { var str = ""; var elements = document.getElementsByTagName('input'); // loop through all input elements in form for(var i = 0; i < elements.length; i++) { // check if element is mandatory; ie has a pattern var pattern = elements.item(i).getAttribute('pattern'); if (pattern != null) { var value = elements.item(i).value; // validate the value of this element, using its defined pattern var offendingChar = value.match(pattern); // if an invalid character is found or the element was left emtpy if(offendingChar != null || value.length == 0) { // add up all error messages str += elements.item(i).getAttribute('errorMsg') + "\n" ; // notify user by changing background color, in this case to red elements.item(i).style.background = "yellow"; } } } if (str != "") { // do not submit the form alert("ERROR ALERT!!\n" +str); return false; } else { // form values are valid; submit return true; } } the scipt picks out pattern and errorMsg from the HTML code: <input pattern='[^A-Za-z]' errorMsg='You are missing Answer 1 for question $q' name='aanswer$q' type='text' id='aanswer$q' size='95' /> Thanks for the help! Quote Link to comment Share on other sites More sharing options...
web_loone_08 Posted September 26, 2008 Share Posted September 26, 2008 try this..... function validate() { var str = ""; var elements = document.getElementsByTagName('input'); var elements2 = document.getElementsByTagName('textarea'); // loop through all input elements in form for(var i = 0; i < elements.length; i++) { // check if element is mandatory; ie has a pattern var pattern = elements.item(i).getAttribute('pattern'); if (pattern != null) { var value = elements.item(i).value; // validate the value of this element, using its defined pattern var offendingChar = value.match(pattern); // if an invalid character is found or the element was left emtpy if(offendingChar != null || value.length == 0) { // add up all error messages str += elements.item(i).getAttribute('errorMsg') + "\n" ; // notify user by changing background color, in this case to red elements.item(i).style.background = "yellow"; } } // loop through all textarea elements in form for(var ii = 0; ii < elements2.length; ii++) { // check if element is mandatory; ie has a pattern var pattern2 = elements2.item(i).getAttribute('pattern'); if (pattern2 != null) { var value2 = elements2.item(i).value; // validate the value of this element, using its defined pattern var offendingChar2 = value2.match(pattern2); // if an invalid character is found or the element was left emtpy if(offendingChar2 != null || value2.length == 0) { // add up all error messages str += elements2.item(i).getAttribute('errorMsg') + "\n" ; // notify user by changing background color, in this case to red elements2.item(i).style.background = "yellow"; } } } 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.