-1

I would like to run my page to test out whether my validation works. However, it does not work with email address. I have added regular expression for email address. It doesn't validate fully.

I entered a@live without typing .com it able to accept it. I assume this is because i type input="email" Correct me wrong. Is it due to the wrong regular expression or maybe the way I placed my code?

javascript

function validate(){
     var fname =  document.getElementById('fname').value;
         var lname =  document.getElementById('lname').value;
    var email =  document.getElementById('email').value;
         var emailReg = '/^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/';
    var re = /^[\w ]+$/;
         var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

     if(email=="")
     {
        alert('Please fill in email fields');   
        return false;
     } 
 else if(fname=="")
     {
        alert('Please fill in first name fields');  
        return false;
     } 
     else if(lname=="")
     {
        alert('Please fill in last name fields');   
        return false;
     } 
     return true;
}

register.php

    <form name="registrationForm" method="post" id="registrationForm" class="registrationForm" action="processRegister.php" onclick="validate">
        <div class='input1'>
            <span id="emailAddress-label" class="help"></span> 
              <input class="regemailaddr" id="emailAddress" name="emailAddress" type="email" placeholder="Email Address " value="" required>
        </div>
          <br> 
            <span id="fname-label" style="margin-bottom:" class="help"></span>
                <input id="fname" name="fname" type="text" placeholder="First Name " class="fname" value="" required>
            <span id="lname-label" class="help"></span> <input id="lname" name="lname" type="text" placeholder="Last Name " class="lname" value="<?php echo $user_profile["lname"]; ?>" required> 
          <br>
               <button class="greybtn" type="submit" id="submitButton">Submit</button>
                <button class="cancelbtn" type="button" id="cancelButton" onclick="window.location='#';return false;">Cancel</button>
</form>

1 Answers1

0

You have defined a regular expression

var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

but you do not test against it

var isEmail = emailReg.test(email);
Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • i should test it in the function validate() ? – SitiNuraini Yakob Oct 22 '15 at 04:17
  • Your validate() function is not being called at all in the fiddle. Try putting an alert('called'); as the first line in validate(). Also you don't actually check isValid in your if...else if... logic. You need to do that. There are some issues with making fiddle work with onsubmit. http://stackoverflow.com/questions/30750369/onsubmit-in-jsfiddle-not-working – Eric J. Oct 22 '15 at 04:51