User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 427,382 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,034 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 793 | Replies: 2 | Solved
Reply
Join Date: Oct 2007
Posts: 30
Reputation: Tom Tolleson is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Tom Tolleson Tom Tolleson is offline Offline
Light Poster

Required field validation

  #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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2008
Posts: 375
Reputation: langsor is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 34
langsor langsor is offline Offline
Posting Whiz

Re: Required field validation

  #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
var form = document.getElementById('myform');

for ( var i = 0; i < form.length; i ++ ) {
  if ( /required/.test( form[i].className ) {
    if ( !form[i].value ) { // might have to tweek this some ...
      alert( 'You missed a required field: ' + field.name );
      return false; // test failed, abort form submit
    }
  }
}

// all fields passed the test ...
return true; // or maybe use
form.submit();

What a required field might look like
<input type="text class="required" name="First Name" />

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 2:40 am.
Reply With Quote  
Join Date: Oct 2007
Posts: 30
Reputation: Tom Tolleson is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Tom Tolleson Tom Tolleson is offline Offline
Light Poster

Re: Required field validation

  #3  
Aug 14th, 2008
Yes it did. Thanks!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JavaScript / DHTML / AJAX Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 4:09 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC