0

In my code address == "" is not being checked by the If function, where I am unable to validate my form input.

if (address == "") {
    document.getElementById('errormessage').style.display = "block";
    window.location = '#errormessage';
    return false;
    }

function validateRegistration() {
 
 var memid = document.forms["registrationForm"]["memid"].value;
 var fname = document.forms["registrationForm"]["fname"].value;
 var lname = document.forms["registrationForm"]["lname"].value;
 var contactno = document.forms["registrationForm"]["contactno"].value;
 var address = document.forms["registrationForm"]["address"].value;
 var email = document.forms["registrationForm"]["email"].value;
 var password = document.forms["registrationForm"]["password"].value;
 var confirmpassword = document.forms["registrationForm"]["confirmpassword"].value;
 
 console.log ("address = "+ address);
 
 if (memid == "" || fname == "" || lname == "" || contactno == "") {
  document.getElementById('errormessage').style.display = "block";
  window.location = '#errormessage';
  return false;
 }
 
 if (address == "") {
  document.getElementById('errormessage').style.display = "block";
  window.location = '#errormessage';
  return false;
 }
 
 if (email == "" || password == "" || confirmpassword == "") {
  document.getElementById('errormessage').style.display = "block";
  window.location = '#errormessage';
  return false;
 }

 
 
}
<div class="col-md-12">
<div id="successmessage" style="text-align: center; background-color: green; color: white; padding: 5px; font-weight: 600; display: none;">
    <a href="login.php">Account Created Successfully (Click to Login)</a>
</div>
                                                   
<div id="errormessage" style="text-align: center; background-color: #f56363; color: white; padding: 5px; font-weight: 600; display: none;">
     <p style="margin: 0px;">Please Fill in all Required Fields</p>
</div>
                                                   
             <?php
             if (isset($_GET['registration'])) {
              $registration = $_GET['registration'];

              if ($registration == 'success') {
               echo '<script type="text/javascript"> document.getElementById("successmessage").style.display ="block"; </script>';
              }
             }
             ?>
                                                    
<div class="company-detail new-account bg-light">
   <div class="new-user-head">
      <h2>Create New Account</h2>
      <span class="underline left"></span>
      <p>Please fill this Registration Form to Register.</p>
   </div>
<form class="login" name="registrationForm" method="post" action="includes/register.inc.php" onSubmit="return validateRegistration()">
               <p class="form-row form-row-first input-required">
                <label>
                 <span class="first-letter">Student ID / Lecturer ID</span>  
                 <span class="second-letter">*</span>
                </label>
                <input type="text" id="memid" name="memid" class="input-text">
               </p>
               <p class="form-row input-required">
                <label>
                 <span class="first-letter">First Name</span>  
                 <span class="second-letter">*</span>
                </label>
                <input type="text" id="fname" name="fname" class="input-text">
               </p>

               <p class="form-row input-required">
                <label>
                 <span class="first-letter">Last Name</span>  
                 <span class="second-letter">*</span>
                </label>
                <input type="text" id="lname" name="lname" class="input-text">
               </p> 

               <p class="form-row input-required">
                <span class="first-letter">Gender: </span> 
                <input type="radio" id="genderMale" name="gender" value="Male" class="input-text"> Male
                <input type="radio" id="genderFemale" name="gender" value="Female" class="input-text"> Female
                <input type="radio" id="genderNSpecified" name="gender" value="NotSpecified" class="input-text" checked> Not Specified
               </p>   

               <p class="form-row input-required">
                <label>
                 <span class="first-letter">Contact Number</span>  
                 <span class="second-letter">*</span>
                </label>
                <input type="text" id="contactno" name="contactno" class="input-text">
               </p> 

               <p class="form-row input-required">
                <label>
                 <span class="first-letter">Address</span>  
                 <span class="second-letter">*</span>
                </label>
                <input type="text" id="address" name="address" class="input-text">
               </p>

                                                                                

               <p class="form-row input-required">  
                <p id="fileName" style="display: inline;"> Choose Profile Image: </p>

                <input type="file" id="imgProfile" name="imgProfile" accept="image/jpeg, image/x-png" class="input-text" style="display: inline;">
               </p>
                                                            
                                                            <p class="form-row input-required" style="margin-bottom: 0px;">
                <label>
                 <span class="first-letter">Email</span>  
                 <span class="second-letter">*</span>
                </label>
                <input type="email" id="email" name="email" class="input-text">
                <p style="margin-top: 0px; margin-bottom: 20px; color: #f56363;">* Your Email will be your username to Login to your account.</p> 
               </p> 
                                                                                                                                                                                       
                                                                                                                             
               <p class="form-row input-required">
                <label>
                 <span class="first-letter">Password</span>  
                 <span class="second-letter">*</span>
                </label>
                <input type="password" id="password" name="password" class="input-text">
               </p>                                                                                                                                             <p class="form-row input-required" style="margin-bottom: 0px;">
                <label>
                 <span class="first-letter">Confirm-Password</span>  
                 <span class="second-letter">*</span>
                </label>
                <input type="password" id="confirmpassword" name="confirmpassword" class="input-text">
                
                <p style="margin-top: 0px; margin-bottom: 20px; color: #f56363; display: none;">* Passwords does not match.</p>
               </p>                                                       
                                                                                                                                                                                                                       
                                                                                                                                                                                                                        <div class="clear"></div>
<input type="submit" value="Signup" name="submit" class="button btn btn-default">
<div class="clear"></div>
     </form> 
</div>
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
Umar Uzman
  • 71
  • 1
  • 1
  • 6
  • 1
    Are you getting any errors? Does document.forms["registrationForm"]["address"].value actually produce a value? – Keith Sep 10 '19 at 18:53
  • Are the returns above that check running? I think you're going to need to supply more context and information about what you want to happen and what's actually happening. – Carcigenicate Sep 10 '19 at 18:54
  • 2
    You have separate `if` statements, each with their own `return`. If one of those `if` statements is true, the function returns and none of the others are executed. Use `else if` and combine the statements and see the linked duplicate for more details. – Scott Marcus Sep 10 '19 at 18:57
  • 1
    It's also unclear why you have 3 separate `if` statements when the true branch of each does the exact same thing. Just combine all the tests into one. – Scott Marcus Sep 10 '19 at 19:11

0 Answers0