troubleshooting Question

Help with javascript form validation.

Avatar of cookiejest
cookiejest asked on
JavaScript
10 Comments1 Solution540 ViewsLast Modified:
I have got a basic javascript code that should return a value to a div depending on what the error is. I have recently modified to include an email validation function and it is not working now.

Also for some reason the termsandconditions field if statement is not working properly as it always returns the error message. Any help would be greatly appreciated!

function validateEmail(email)
{
// a very simple email validation checking.
// you can add more complex email checking if it helps
    if(email.length <= 0)
        {
          return true;
        }
    var emailCheck = validateAddress(email); // call this function here!
    if(emailCheck)
    {
      var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
      if(splitted[2].match(regexp_domain) == null)
      {
            var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
            if(splitted[2].match(regexp_ip) == null) return false;
      }// if
      return true;
    }
return false;
}

function validate(){

      if(document.signup_form.username.value.length < 3 ){
    document.getElementById('txtsignupformHint').innerHTML = 'Please enter a valid username that is at least 3 digits long';
      } else if(validateEmail(document.signup_form.email.value) == false ){
    document.getElementById('txtsignupformHint').innerHTML = 'Please enter a valid email address';
      } else if(document.signup_form.password.value.length < 6 ){
      document.getElementById('txtsignupformHint').innerHTML = 'Please enter a password that is at least 6 digits long';
    } else if(document.signup_form.passwordcheck.value.length < 6 ){
      document.getElementById('txtsignupformHint').innerHTML = 'Please enter the password again.';
    } else if(document.signup_form.first_name.value == ""){
      document.getElementById('txtsignupformHint').innerHTML = 'Please enter a first name.';
    } else if(document.signup_form.surname.value == ""){
      document.getElementById('txtsignupformHint').innerHTML = 'Please enter a Surname name.';
    } else if(document.signup_form.captcha_code.value.length < 4 ){
      document.getElementById('txtsignupformHint').innerHTML = 'Please enter the 4 digit security code.';
      } else if(!document.signup_form.termsandconditions.checked){
      document.getElementById('txtsignupformHint').innerHTML = 'Please agree to the terms and conditions';      
      return false; } else {
    document.signup_form.submit();
      }
}

The code below is the form code:
<form action="signup.php" method="POST" name="signup_form" onSubmit="return validatePwd()" >
<p> Account Info
Username: 
     <input name="username" type="text" value="<?php if(isset($_POST['username'])){echo $_POST['username'];}?>" size="40" maxlength="50" onkeyup="checkSIGNUP(this.value, 'username')" /> 
     <div id="divusername"></div> 
  
  
     Email: 
     <input name="email" type="text" value="<?php if(isset($_POST['email'])){echo $_POST['email'];}?>" size="40" maxlength="50" onchange="checkSIGNUP(this.value, 'email')" /> 
      <div id="divemail"></div>
   
   
    Password: 
    <input name="password" type="password" class="password" /> 
     
   
   
     Password Check 
    <input name="passwordcheck" type="password" class="passwordcheck" /> 
    
   
   
     First Name: 
     <input name="first_name" type="text" value="<?php if(isset($_POST['first_name'])){echo $_POST['first_name'];}?>" size="40" maxlength="50" /> 
      
   
   
     Surname: 
     <input name="surname" type="text" value="<?php if(isset($_POST['surname'])){echo $_POST['surname'];}?>" size="40" maxlength="50" /> 
      
   
   
     Other Names: 
     <input name="other_names" type="text" value="<?php if(isset($_POST['other_names'])){echo $_POST['other_names'];}?>" size="40" maxlength="50" /> 
 
<p>
 
 
 
<img id="captcha" src="securimage/securimage_show.php?sid=<?php echo md5(uniqid(time())); ?>" alt="Security Image" />
<a href="#" onclick="document.getElementById('captcha').src = 'securimage/securimage_show.php?' + Math.random(); return false">Reload Image</a>
<p>
Please enter the code in the image above: 
  <input type="text" name="captcha_code" size="10" maxlength="6" />
<input name="register" type="hidden" value="reg" />
<p>
Do you agree with the <a id="handle" onclick="toggle()" href="#">Terms and Conditions</a>?
<input name="termsandconditions" type="checkbox" />
<p>
<div id="content" style="display: none" ><textarea name="termsandconditions" cols="55" rows="10" readonly> stuff </textarea></div>
  <input type="button" onclick='validate()' name="register" value="register"  />
 
</form>
<div id="txtsignupformHint"></div>
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 10 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 10 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros