Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
simple validation by disabling SUBMIT btn until required fields met
Old 03-29-2010, 02:04 PM simple validation by disabling SUBMIT btn until required fields met
seannarae's Avatar
Skilled Talker

Posts: 65
Location: san francisco, ca
Trades: 0
I'm creating a prototype for an Opt-In page, with a checkbox for the actual opt-in, another checkbox to receive a confirmation letter, and a radio group for selecting the delivery method for that letter.

The SUBMIT button is disabled onload. The Opt-in checkbox is the only required element in that when ticked, we should enable the SUBMIT button.

Where i am stumped is how to add a secondary function to the radio group. Meaning: if the user ticks the confirmation letter checkbox, yet doesnt select one of the subsequent radio-buttons, the form is incomplete and therefore the SUBMIT button should be disabled.

I'm afraid for the confirmation letter, i must use this checkbox + radio group (a regulatory requirement [?]).

I've had success in the past with more basic IF/ELSE functions on single form elements. But i'm stumped on how to have multiple arguments (and, "||") within.

As in previous posts, I just need assistance getting past hurdles of syntax. Thanks for reading, suggestions, hints, etc.

TEST PAGE:
http://bit.ly/9I4eZh

HEAD:
Code:
<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function() {

    // enables/disables or shows/hides element(s) based on user-actions with other element(s)
    $("#btn_submit").attr("disabled", true);
    $('#check_optin').attr('checked', false);

    function check_elements () {
        if (
            $('#check_optin').is(':checked') ||
            $('#check_conflet').is(':checked') ||
            $("input[name='del_set']").is(':checked')
        )
            {$('#btn_submit').removeAttr('disabled');}
        else {$("#btn_submit").attr("disabled", true);}
    }})
</script>
BODY:
Code:
<p>
    <input type="checkbox" id="check_optin" />
    <label for="check_optin" class="checkbox"><strong>I would like to opt-out of the service.</strong></label>
</p>

<p>
    <input type="checkbox" id="check_conflet" />
    <label for="check_conflet" class="checkbox"><strong>Please send a confirmation letter by:</strong></label>
</p>

<p>
    <input type="radio" name="del_set" value="email" /> Email<br />
    <input type="radio" name="del_set" value="usps" /> First Class Mail<br />
</p>

<button type="submit" id="btn_submit">Submit</button>
seannarae is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-02-2010, 07:19 PM Re: simple validation by disabling SUBMIT btn until required fields met
Skilled Talker

Posts: 54
Trades: 0
if i understood your problem you want to enable the submit button if and only if all the form fields are set. i have this function for you to check for that:
Code:
<script language="javascript" type="text/javascript">
function validateformelements(formID,checkID){
var confirmcheck=document.getElementById(checkID);
if(confirmcheck.checked){
var message="";
var group=document.getElementById(obj);
var inputs = group.getElementsByTagName("input");
    var selects = group.getElementsByTagName("select");
    for (var i = 0; i < inputs.length; i++) {
         message+= (inputs[i].value) ? "" :"Please complete the form inputs";
   }
   
    for (var i = 0; i < selects.length; i++) {
         message+= (selects[i].value) ? "": "Please complete the form select";
    }
 if(message==""){
var btn=document.getElementById('btn_submit');
btn.disable="true"; 

 }else{
 alert("Please complete all the form inputs");
 
 }
}
}
</script>
html code
HTML Code:
<form action="" method="post" name="form1" id="form1" ><input type="text" name="txt1" value="" /><input type="text" name="txt2" value=""/><select name="select1"><option value="option1">1</option>"><option value="option2">2</option></select>
confirmation checkbox<input type="checkbox" name="confirmation" id="confirm" onchange="validateformelements('form1','confirm')"/><input type="submit"  value="send" id="btn_submit"/></form>
hopes it helps.
kani alavi is offline
Reply With Quote
View Public Profile
 
Old 04-06-2010, 02:42 PM Re: simple validation by disabling SUBMIT btn until required fields met
seannarae's Avatar
Skilled Talker

Posts: 65
Location: san francisco, ca
Trades: 0
@kani

"...if and only if all the form fields are set"
No actually, thats not quite what i was seeking here.

I'm seeking syntax assistance with jQuery. The only required element is the opt-in checkbox. Ticking that is enough to enable the SUBMIT button, since the Confirmation Letter checkbox is optional.

However, if the user does tick the Confirmation Letter checkbox, we now have additional required elements: the radio group for Delivery Method. The SUBMIT button in this case would be disabled until their either untick the box or select a radio button.

I can make all fields required. Where i am confused is syntax: how to format the conditional relationship between the radio group & the SUBMIT button.

Thanks for your time & suggestions though...
seannarae is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to simple validation by disabling SUBMIT btn until required fields met
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.12117 seconds with 12 queries