Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for valid email before running remaining javascript

I have a textbox where the user is required to insert a valid email address.

When the user submits a valid email address a loading graphic appears while the data is posted back.

The code below works fine for showing the loading graphic but it does not check that the email address is valid first. Can anyone help out?

$('#btnEmail1Submit').live ("click", function() {
   $('<div class="submitBg"></div>').appendTo(".emailEditContainer");
   $('<div class="submitLoadingCont"><img class="submitLoading" src="images/mypreferences/loading.gif" width="50" height="50" /></div>').appendTo(".emailEditContainer");
});

I am thinking that I need to put an if statement around the function that is run on click - so something like:

$('#btnEmail1Submit').live ("click", function() {
  if(emailvalid == true) {   
    $('<div class="submitBg"></div>').appendTo(".emailEditContainer");
    $('<div class="submitLoadingCont"><img class="submitLoading" src="images/mypreferences/loading.gif" width="50" height="50" /></div>').appendTo(".emailEditContainer");
  }
});

I am using asp.net email validation - it looks something like this:

<asp:RegularExpressionValidator Display="Dynamic" ValidationGroup="PrimarySubmit" ID="RegularExpressionValidator1" runat="server" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ControlToValidate="tbEmail1" ErrorMessage="Invalid email address - " />
like image 489
Tom Avatar asked Jun 30 '11 10:06

Tom


2 Answers

You should check the email validity using a regexp

var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/

$('#btnEmail1Submit').live ("click", function() {
   if(!email.match(re)) {
     alert('invalid email');
     return false;
   }
   $('<div class="submitBg"></div>').appendTo(".emailEditContainer");
   $('<div class="submitLoadingCont"><img class="submitLoading" src="images/mypreferences/loading.gif" width="50" height="50" />    </div>').appendTo(".emailEditContainer");
});

The regexp comes from Validate email address in JavaScript?

like image 111
olivieradam666 Avatar answered Oct 15 '22 23:10

olivieradam666


You will need to use a regex to test the email address for validity:

function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    return pattern.test(emailAddress);
};

That came from this question, so see that thread for more info.

You need to call that function with the email address provided by the user, so I'm assuming something like:

var email = $("#emailInput").val();
if(isValidEmailAddress(email)) {
    //Do stuff
}
like image 14
James Allardice Avatar answered Oct 16 '22 00:10

James Allardice