•
•
•
•
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
![]() |
•
•
Join Date: Oct 2007
Posts: 30
Reputation:
Rep Power: 2
Solved Threads: 0
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
Thanks,
Tom Tolleson
•
•
Join Date: Aug 2008
Posts: 375
Reputation:
Rep Power: 1
Solved Threads: 34
You can test the field by className in your javascript and give all required fields the same class name.
Something like this
What a required field might look like
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
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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb JavaScript / DHTML / AJAX Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- JavaScript form validation - please help (JavaScript / DHTML / AJAX)
- question about required field validator (ASP.NET)
- .net validation/javascript conflict (ASP.NET)
- preg_match validation (PHP)
- Empty field prevention...pop-ups possible? (PHP)
- form validation not working (JavaScript / DHTML / AJAX)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: why won't this script work?
- Next Thread: Are JavaScript namespaces a bad thing?


Linear Mode