Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Oct 2004
    Posts
    104
    Thanks
    7
    Thanked 2 Times in 2 Posts

    Disabling 'submit'

    Hi there,
    I'm trying to create a simple check and act sorta thing. I want it, so that if 'user' and/or 'email' is blank, then disable the submit button, else enable it. I got the following code, but its failing to do what I want:

    Code:
    <script type="text/javascript" language="JavaScript">
    function required() {
    	var check
    	if(document.updateaccount.user.value == "") {
    		check = 1;
    	}
    	if(document.updateaccount.email.text == "") {
    		check = 1;
    	}
    	if(check == 1) {
    		document.updateaccount.Submit.disabled = "true";
    	} else {
    		document.updateaccount.Submit.disabled = "false";
    	}
    }
    </script>
    Can anyone help me please?

    Thanks
    NP

  • #2
    Senior Coder BarrMan's Avatar
    Join Date
    Feb 2005
    Location
    Israel.
    Posts
    1,644
    Thanks
    69
    Thanked 83 Times in 82 Posts
    What does it fail to do? Perform the validation or validate the form on time?
    Can you send the form code?

  • #3
    Regular Coder
    Join Date
    Oct 2004
    Posts
    104
    Thanks
    7
    Thanked 2 Times in 2 Posts
    This is the form. Check remains undefined which is good because the fields are not blank. The submit button, still remains disabled though.

    Heres the form (please note it does include some php)
    Code:
      <form action="" method="post" name="updateaccount" id="updateaccount">
        <div align="left">
          <p>Username: <strong><font color="#FF0000">*</font></strong><br>
            <input name="user" type="text" id="user" value="<?=$_SESSION['user']?>" size="50" onChange="required()">
            <img src="images/blank.jpg" width="15" height="15" border="1"> </p>
          <p>Current Password: <strong><font color="#FF0000">*</font></strong><br>
            <input name="pass" type="password" id="pass" size="50">
            <img src="images/blank.jpg" name="currpass" width="15" height="15" border="1" id="currpass"> </p>
          <p>New Password: <em>(Please only fill in if you wish to change your current
              password)<br>
            <input name="newpass1" type="password" id="newpass1" size="50">
              </em></p>
          <p>Confirm New Password: <em>(Only required if you filled in 'New Password'
              above)</em><br>
              <input name="newpass2" type="password" id="newpass2" size="50">
              <img src="images/blank.jpg" name="confpass" width="15" height="15" border="1" id="confpass"></p>
          <p>EMail Address: <strong><font color="#FF0000">*</font></strong><br>
            <input name="email" type="text" id="email" value="<?=$_SESSION['email']?>" size="50" onChange="required()"> 
          <img src="images/blank.jpg" name="email" width="15" height="15" border="1" id="email"></p>
          <p>Gender:<br>
            <?php
    		$gender = '<select name="gender">
            <option value="male">Male
            <option value="female">Female
            <option value="disclosed">Disclosed        
            </select>';
    		$gender = str_replace('value="'.$_SESSION['gender'].'">', 'value="'.$_SESSION['gender'].'" selected>', $gender);
    		echo $gender;
    		?>
    </p>
          <p>
            <input type="submit" name="Submit" value="Update Account Details" disabled="false"></p>
        </div>
      </form>

  • #4
    Senior Coder
    Join Date
    Feb 2003
    Posts
    1,665
    Thanks
    0
    Thanked 27 Times in 25 Posts
    Quote Originally Posted by NeoPuma View Post
    Code:
    <script type="text/javascript" language="JavaScript">
    function required() {
    	var check
    	if(document.updateaccount.user.value == "") {
    		check = 1;
    	}
    	if(document.updateaccount.email.text == "") {
    		check = 1;
    	}
    	if(check == 1) {
    		document.updateaccount.Submit.disabled = "true";
    	} else {
    		document.updateaccount.Submit.disabled = "false";
    	}
    }
    </script>
    Checking for the disabled value returns a boolean value of either true or false. By placing true and false within quotes, you are referencing them as a 'string literal'.

    Code:
    function required() {
    	var check;
    	if ((document.updateaccount.user.value == "") || (document.updateaccount.email.text == "") {
    		check = 1;
    	}
    	if (check == 1) {
    		document.updateaccount.Submit.disabled = true;
    	} else {
    		document.updateaccount.Submit.disabled = false;
    	}
    }
    As the earlier check can also return a true/false boolean value, it could be referenced more directly, with the script written as follows…
    Code:
    function required() {
    	var check;
    	if ((document.updateaccount.user.value == "") || (document.updateaccount.email.text == "")) {
    		check = 1;
    	}
    	document.updateaccount.Submit.disabled = (check == 1);
    }
    You could even go a step further and use the first check to power the final value of the submit button's disabled value.
    Code:
    function required() {
    	document.updateaccount.Submit.disabled = ((document.updateaccount.user.value == "") || (document.updateaccount.email.text == ""));
    }
    HTH


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •