| | |
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 |
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: 381
Reputation:
Solved Threads: 33
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
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
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
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<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 3:40 am.
![]() |
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?
| Thread Tools | Search this Thread |
ajax ajaxcode ajaxexample ajaxhelp ajaxjspservlets animate automatically browser bug calendar captchaformproblem checkbox child class close cookies createrange() cursor date debugger dependent disablefirebug dom dropdown editor element embed engine events explorer ext file form forms getselection google gxt hiddenvalue highlightedword hint html htmlform ie7 ie8 iframe image() images internet java javascript javascripthelp2020 jawascriptruntimeerror jquery jsf jsfile jump libcurl maps math media microsoft object onerror onmouseoutdivproblem onreadystatechange parent paypal pdf php player position post programming progressbar rated regex runtime safari scriptlets scroll search security session shopping size software sql star stars synchronous text textarea unicode validation web webservice website windowsxp wysiwyg \n





