0

So I got this js code for a form submission, and everything is working fine, however, I don't know where and what to write in the code so the email gets a validation check. What and where should I write to validation check the email?

$(document).ready(function() {
  $("#submit").click(function() {

    var name = $("#fullname2").val();
    var email = $("#fullemail2").val();
    var state = $("#selectstate").val();
    // Returns successful data submission message when the entered information is stored in database.
    var dataString = 'FullName=' + name + '&email=' + email + '&SovereignState=' + state;
    if (name == '' || email == '' || state == '') {
      $('#required_fields').show();
    } else {
      // AJAX Code To Submit Form.
      $.ajax({
        type: "POST",
        url: "demo.php",
        data: dataString,
        cache: false,
        success: function(phpSays) {
          if (phpSays == "OK") {
            $('#email_error').show();
            $('#required_fields').hide();
          } else {
            $('#sinatra2').hide();
            $('#thanks').fadeIn(1000);
            $('#spreading_message').delay(1800).fadeIn(1500);
            $('#by_social').delay(3000).fadeIn(1500);
            $('#email_error').hide();
            $('#required_fields').hide();
          }
        }
      });
    }
    return false;
  });
});
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Chris
  • 21
  • 1
  • 7
  • possible duplicate http://stackoverflow.com/questions/2855865/jquery-regex-validation-of-e-mail-address – Muhammad Usman Sep 23 '15 at 04:09
  • 1
    possible duplicate of [Validate email address in JavaScript?](http://stackoverflow.com/questions/46155/validate-email-address-in-javascript) – Elias Nicolas Sep 23 '15 at 04:26

3 Answers3

2

Looking at your code I can suggest the below approach to say where you can do email validation

if(name==''||email==''||state=='')
{
   $('#required_fields').show();
}//this is fine
else if(!valid(email))//call a function which validates email and returns true or false
{
    //Display the message either with same $('#required_fields') changing the text or 
    //Add one more field to display invalid email message
}
else
{
    //Your ajax call here
}

Now your valid function will look like

function valid(email)
{
    var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
    return emailReg.test(email); //this will either return true or false based on validation
}
Guruprasad J Rao
  • 29,410
  • 14
  • 101
  • 200
1
 $(document).ready(function(){
 $("#submit").click(function(){

 var name = $("#fullname2").val();
 var email = $("#fullemail2").val();
 var state = $("#selectstate").val();
 // Returns successful data submission message when the entered information is stored in database.
 var dataString = 'FullName='+ name + '&email='+ email + '&SovereignState='+ state;
 if(name==''||email==''||state=='')
 {
 $('#required_fields').show();
 }
 else
 {
 // AJAX Code To Submit Form.
   // <-- email address should be here
   ...........    
 }
 return false;
 });
 });
The KNVB
  • 3,588
  • 3
  • 29
  • 54
1

Better place to validate is where you have 'fullemail2' input field. Even in the javascript file, you should do it before create the dataString. In that way you could validate before submit.