We help IT Professionals succeed at work.

Validating an email address

lulu50
lulu50 asked
on
Hi,

I need your help please.

I'm trying to validate an email address

function ValidateData() {
        //debugger;
        var StartCreateDate = $('#FromCreatedDate').val();
        var EndCreateDate = $('#ToCreatedDate').val();
        var StartMigrationDate = $('#FromMigrationDate').val();
        var EndMigrationDate = $('#ToMigrationDate').val();
        var SendToEmail = $('#SendEmailTo').val();
       
        if ((StartCreateDate != null && EndCreateDate != null) && (StartCreateDate > EndCreateDate)) {
            alert("From create date must be greater than to create date")
            return false;
        }
        if ((StartMigrationDate != null && EndMigrationDate != null) && (StartMigrationDate > EndMigrationDate)) {
            alert("From migration date must be greater than to migration date")
            return false;
        }
        if (SendToEmail "doesn't have the correct format")  //Here I want to validate my email address $('#SendEmailTo').val();
        {
            alert("Email format is incorrect")
            return false;
        }

        return true;
    }

Open in new window



Thank you,
Comment
Watch Question

leakim971Multitechnician
SILVER EXPERT
Distinguished Expert 2019

Commented:
old post :
https://www.experts-exchange.com/questions/26396198/validating-an-email-address.html#a33414276
https://www.experts-exchange.com/questions/28468825/Email-Validation.html#a40174778


function ValidateData() {
        //debugger;
        var StartCreateDate = $('#FromCreatedDate').val();
        var EndCreateDate = $('#ToCreatedDate').val();
        var StartMigrationDate = $('#FromMigrationDate').val();
        var EndMigrationDate = $('#ToMigrationDate').val();
        var SendToEmail = $('#SendEmailTo').val();
       
        if ((StartCreateDate != null && EndCreateDate != null) && (StartCreateDate > EndCreateDate)) {
            alert("From create date must be greater than to create date")
            return false;
        }
        if ((StartMigrationDate != null && EndMigrationDate != null) && (StartMigrationDate > EndMigrationDate)) {
            alert("From migration date must be greater than to migration date")
            return false;
        }
        if ( emailHaveCorrectFormat(SendToEmail) )  //Here I want to validate my email address $('#SendEmailTo').val();
        {
            alert("Email format is incorrect")
            return false;
        }

        return true;
    }

function emailHaveCorrectFormat(email) {
      var filter = ^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$;
      return filter.test(email.value);
}

Open in new window

Bill PrewTest your restores, not your backups...
SILVER EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Try using a regular expression pattern, that's a pretty common approach.  Of course, if you google around you will find a variety of patterns for validating email addresses, some too simple, some too complex, you will have to judge your needs.  Here's an example one.

function ValidateData() {
        //debugger;
        var StartCreateDate = $('#FromCreatedDate').val();
        var EndCreateDate = $('#ToCreatedDate').val();
        var StartMigrationDate = $('#FromMigrationDate').val();
        var EndMigrationDate = $('#ToMigrationDate').val();
        var SendToEmail = $('#SendEmailTo').val();
        var ValidEmail = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
       
        if ((StartCreateDate != null && EndCreateDate != null) && (StartCreateDate > EndCreateDate)) {
            alert("From create date must be greater than to create date")
            return false;
        }
        if ((StartMigrationDate != null && EndMigrationDate != null) && (StartMigrationDate > EndMigrationDate)) {
            alert("From migration date must be greater than to migration date")
            return false;
        }
        if (!ValidEmail.test(SendToEmail))
        {
            alert("Email format is incorrect")
            return false;
        }

        return true;
    }

Open in new window


»bp
lulu50Web application

Author

Commented:
Thank you guys for your help


I'm getting this error



Parser Error
  Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

 Parser Error Message: "[" is not valid at the start of a code block.  Only identifiers, keywords, comments, "(" and "{" are valid.


Source Error:



Line 110:
Line 111:    function emailHaveCorrectFormat(email) {
Line 112:        var filter = ^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$;
Line 113:        return filter.test(email.value);
Line 114:    }
lulu50Web application

Author

Commented:
the same error I try the other one


Parser Error
  Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

 Parser Error Message: "[" is not valid at the start of a code block.  Only identifiers, keywords, comments, "(" and "{" are valid.


Source Error:



Line 124:        var EndMigrationDate = $('#ToMigrationDate').val();
Line 125:        var SendToEmail = $('#SendEmailTo').val();
Line 126:        var ValidEmail = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
Line 127:
Line 128:
Bill PrewTest your restores, not your backups...
SILVER EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Not sure your environment, but in a couple of bare bones tests here the part I suggested seems to work...

https://jsfiddle.net/wbtzo45r/

http://tpcg.io/UPQcnK3G


»bp
leakim971Multitechnician
SILVER EXPERT
Distinguished Expert 2019

Commented:
I miss the slahes :

function emailHaveCorrectFormat(email) {
    var filter = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
    return filter.test(email.value);
}

Open in new window

lulu50Web application

Author

Commented:
I still get the same error
it's looking for this (  )



Parser Error
  Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

 Parser Error Message: "[" is not valid at the start of a code block.  Only identifiers, keywords, comments, "(" and "{" are valid.


Source Error:



Line 110:
Line 111:function emailHaveCorrectFormat(email) {
Line 112:    var filter = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
Line 113:    return filter.test(email.value);
Line 114:}
 

 Source File:  /Views/CareAdvanceBusinessRules/Management.cshtml    Line:  112
Multitechnician
SILVER EXPERT
Distinguished Expert 2019
Commented:
it's an issue with your code editor not with the code itself or maybe another block of your page
lulu50Web application

Author

Commented:
yes I agree Thank you so much
lulu50Web application

Author

Commented:
Thank you
leakim971Multitechnician
SILVER EXPERT
Distinguished Expert 2019

Commented:
you welcome