Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have the below js code to test email

function testMatch() {
var current = $(document).find('#txtbox');
var ccount = 0;
var vcode = "/^[a-z0-9\_\-\.]+[\@]{1}[a-z0-9]+[\.]{1}[a-z]{2,}/i" /*this value actually comes from a json request*/
var value = "useremail@gmail.com";
if (value != undefined && value != null && value != '') {
var res = value.match(vcode);
if (res == null) {
$(current).addClass('cm-dataform-errorrow');
ccount++;
} else {
$(current).removeClass('cm-dataform-errorrow');
}
} else {
$(current).addClass('cm-dataform-errorrow');
ccount++;
}
}

the regex string is fetched from the ajax request.
since the regex exp is a string the match() function does not work.
How do i convert string regex exp to proper regex exp which can validate data input here.

What I have tried:

I tried using test(), exec() methods of RegExp
Posted
Updated 4-Apr-17 2:32am
Comments
Wessel Beulink 4-Apr-17 3:22am    
/^[a-z0-9\_\-\.]+[\@]{1}[a-z0-9]+[\.]{1}[a-z]{2,}/i this is also not a valid mail verification. because of the new domains: .black etc.

Microsoft recomending the following regex:
@"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" + @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$"

See this article: https://msdn.microsoft.com/en-us/library/01escwtf(v=vs.110).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1

But I should simply check on a @ in with String.Contain('@'). You could also check for a dot.

You have to create a RegExp object:
JavaScript
var re = new RegExp(vcode, 'i');
var res = value.match(re);
Note that the flags are passed as extra parameter to the RegExp constructor and must be therefore removed from the vcode string.

See also RegExp - JavaScript | MDN[^]
 
Share this answer
 
Comments
Christopher Fernandes 4-Apr-17 6:02am    
I implemeted your code this is what the re look like when the code executes /\/^[a-z0-9\_\-\.]+[\@]{1}[a-z0-9]+[\.]{1}[a-z]{2,}\//i
it does not work i mean even if i enter a valid email.
Jochen Arndt 4-Apr-17 6:11am    
You have escaped all special characters!
Your string is already a valid regex (hopefully; I did not checked it). Pass it as it is but remove the trailing "i":

/^[a-z0-9\_\-\.]+[\@]{1}[a-z0-9]+[\.]{1}[a-z]{2,}/
Christopher Fernandes 4-Apr-17 7:27am    
I did not escape the characters the escape character are coming auto. it is this escape characters that are creating problems for me here in this regex.
Jochen Arndt 4-Apr-17 7:37am    
I see it now.
There is actual a bug here at CP with the code block formatting (the br tags in code blocks) and it seems there is something similar with the comments.


To check if the regex is working I suggest to start with a simpler one and expand that while it is working.

You may also use an online parser (serach for "online regex tester").

[EDIT]
But there is a difference.
Your string:
/\/^[a-z0-9\_\-\.]+[\@]{1}[a-z0-9]+[\.]{1}[a-z]{2,}\//i
But it must be
/^[a-z0-9\_\-\.]+[\@]{1}[a-z0-9]+[\.]{1}[a-z]{2,}/
[/EDIT]

Where does the auto escaping come from?
The embedding slashes can be also removed:
^[a-z0-9\_\-\.]+[\@]{1}[a-z0-9]+[\.]{1}[a-z]{2,}
Christopher Fernandes 4-Apr-17 8:22am    
That's what the regex pattern is getting treated as a string hence not matching to value.
This is how i fixed it. I striped the first & last forward slash

var ccount = 0;
var vcode = ^[a-z0-9\_\-\.]+[\@]{1}[a-z0-9]+[\.]{1}[a-z]{2,}$
var value = "useremail@gmail.com";
var regex = new RegExp(vcode,'i');
if (value != undefined && value != null && value != '') {
    var res = regex.exec(vcode);
    if (res == null) {
        ccount++;
    } 
    else {
         /*Valid value do stuff here*/
    }
} 
else {
  ccount++;
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900