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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Variables and HTML Forms

    Currently I have the following script:-

    Code:
    function validate_form(){
        if(regform.reguser.value == ""){
    	  alert("Please completed the selected box");
    	  regform.reguser.focus();
    	  return false;  
        }else if(regform.regemail.value == ""){
          alert("Please completed the selected box");
    	  regform.regemail.focus();
    	  return false;
    	}else if(regform.regpass1.value == ""){
    	  alert("Please completed the selected box");
    	  regform.regpass1.focus();
    	  return false;
        }else if(regform.regpass2.value == ""){
    	  alert("Please completed the selected box");
          regform.regpass2.focus();	  
    	  return false;
    	}else{	
          return true;
    	}
    }
    to validate a simple registration form, however I initially tried to streamline this function by cycling through an array using a loop to point to various input elements in the HTML page itself. I found that when trying to use a variable in the aforementioned if statements the javascript failed to work i.e.

    Code:
    var test = "reguser";
    if(regform.test.value = ""){
    }
    I know the javascript is looking for the input element "test" instead of "reguser" but is there any way I can force it to look for the contents of the variable.

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    Code:
    regform.elements[test].value

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,121
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Code:
    var test = "reguser";
    if(regform[test].value = ""){  ... }
    EDIT: Oh, man! THREE minutes. I really am slowing down!
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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