0

I am using RegExp for the validation of US phone number. For ReEmailFormat6 i am getting below error: enter image description here

but other expression running fine. I am not sure why I am this issue . Please find my code below:

    var reEmailFormat2 = new RegExp("(.)\1{7,}"); //match if phone number has same number repeated 8 or more times
var reEmailFormat3 = new RegExp("^0{1}[\d]{9}$"); //match if phone number has leading 0 with 9 digits after the 0
var reEmailFormat4 = new RegExp("1{1}[\d]{9}$"); //match if phone number has leading 1 with 9 digits after the 1
var reEmailFormat5 = new RegExp("/^[+]?(1\-|1\s|1|\d{3}\-|\d{3}\s|)?((\(\d{3}\))|\d{3})(\-|\s)?(\d{3})(\-|\s)?(\d{4})$/g");
var reEmailFormat6 = new RegExp("^(?:\+?1[-. ]?)?\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$");
debugger;

if (controlToValidate[0].value == "" || controlToValidate[0].value == controlToValidate.attr("placeholder")) {
    args.IsValid = false;
    controlToValidate.addClass("site-validation-field");
}
else if (controlToValidate[0].value == "") {
    args.IsValid = false;
    controlToValidate.addClass("site-validation-field");
}
else if ((reEmailFormat6.test(controlToValidate[0].value))) {
    debugger;
    args.IsValid = false;
    controlToValidate.addClass("site-validation-field");
}
5
  • You're getting the error because your regular expression is syntactically incorrect. Also, it's a bad idea to use the RegExp constructor instead of simpler regex literal syntax because you have to deal with additional quoting issues, which your code does not do properly. Commented Mar 13, 2020 at 20:50
  • 1
    And finally: this is a terrible idea, modern email addresses are basically just "anything" followed by @, followed by "anything", followed by as many "dot anything" as the world wants to slap on. Which they can. And do. And in addition: why are your vars about "email" but your comments about "phone"? Commented Mar 13, 2020 at 20:52
  • @Pointy this is the expression i am using in vb working as expected. but same in java script giving error. I can try regex literal ..Thanks !! Commented Mar 13, 2020 at 20:53
  • @Mike'Pomax'Kamermans no, it's not "modern" email addresses. This has been the case for a VERY long time. It's just that many people completely ignored the standard when writing validations. Commented Mar 13, 2020 at 20:59
  • + is a meta-character in regex. To use its literal value you have to escape it (\+). Commented Mar 13, 2020 at 20:59

1 Answer 1

1

Rewrite all of your regular expressions with regular expression literal syntax:

var reEmailFormat2 = /(.)\1{7,}/; //match if phone number has same number repeated 8 or more times
var reEmailFormat3 = /^0{1}[\d]{9}$/; //match if phone number has leading 0 with 9 digits after the 0
var reEmailFormat4 = /1{1}[\d]{9}$/; //match if phone number has leading 1 with 9 digits after the 1
var reEmailFormat5 = /^[+]?(1\-|1\s|1|\d{3}\-|\d{3}\s|)?((\(\d{3}\))|\d{3})(\-|\s)?(\d{3})(\-|\s)?(\d{4})$/g;
var reEmailFormat6 = /^(?:\+?1[-. ]?)?\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;

When you form your regular expressions as strings to pass to the RegExp() constructor, you have to take into account the fact that the string syntax in JavaScript also uses the backslash character as a meta-character. Thus if you don't double your backslash characters in the source string, the process of parsing it as a string will remove them.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.