Jquery RegEx Validation

前端 未结 5 508
情书的邮戳
情书的邮戳 2020-12-31 10:52

I am wanting to check if an input field has the attribute \"pattern\" and if so, preform a regex check aganst said pattern.I know this is already done by HTML5, but I am wan

5条回答
  •  感动是毒
    2020-12-31 11:31

    You can try something like this. It will alert "var2 failed".

    var var1 = "1";
    var var2 = "A";
    var myRegEx = new RegExp('[0-9]+'); //make sure the var is a number
    
    if (var1 != myRegEx.exec(var1)) {
        alert("var1 failed");
    }
    
    if (var2 != myRegEx.exec(var2)) {
        alert("var2 failed");
    }
    

提交回复
热议问题