Jump to content

Updating form validation


ifis

Recommended Posts

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!

Link to comment
Share on other sites

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";  
     } 
   } 

}  

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.