Required field validation

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: Oct 2007
Posts: 39
Reputation: Tom Tolleson is an unknown quantity at this point 
Solved Threads: 0
Tom Tolleson's Avatar
Tom Tolleson Tom Tolleson is offline Offline
Light Poster

Required field validation

 
0
  #1
Aug 8th, 2008
I have an html form with multiple fields. Some of the fields are required, others are not. I have a JavaScript validation form that will send an alert if a field is empty. However, I only want it to send an alert if a required field is empty. How do I signify which fields in the form should cause the alert message to pop up?

Thanks,

Tom Tolleson
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 381
Reputation: langsor is an unknown quantity at this point 
Solved Threads: 33
langsor langsor is offline Offline
Posting Whiz

Re: Required field validation

 
0
  #2
Aug 9th, 2008
You can test the field by className in your javascript and give all required fields the same class name.

Something like this
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var form = document.getElementById('myform');
  2.  
  3. for ( var i = 0; i < form.length; i ++ ) {
  4. if ( /required/.test( form[i].className ) {
  5. if ( !form[i].value ) { // might have to tweek this some ...
  6. alert( 'You missed a required field: ' + field.name );
  7. return false; // test failed, abort form submit
  8. }
  9. }
  10. }
  11.  
  12. // all fields passed the test ...
  13. return true; // or maybe use
  14. form.submit();

What a required field might look like
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <input type="text class="required" name="First Name" />
  2.  

This is off the top of my head, so I don't know it will work "out of the box" but it's the general idea anyway.

Hope this helps
Last edited by langsor; Aug 9th, 2008 at 3:40 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 39
Reputation: Tom Tolleson is an unknown quantity at this point 
Solved Threads: 0
Tom Tolleson's Avatar
Tom Tolleson Tom Tolleson is offline Offline
Light Poster

Re: Required field validation

 
0
  #3
Aug 14th, 2008
Yes it did. Thanks!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC