jquery - Using Regex with JqueryUI Validate -
i trying add regex validation jqueryui validator following guidance of jquery validate: how add rule regular expression validation?
$.validator.addmethod( "regex", function(value, element, regexp) { var re = new regexp(regexp); return this.optional(element) || re.test(value); }, "please check input." );
and
$("#textbox").rules("add", { regex: "^[a-za-z'.\\s]{1,40}$" })
the regex include
^(\d+(?:(?: \d+)*\/\d+)?)$
which can seen @ http://regex101.com/r/cb3fq1/2
when enter regex jquery, no entries pass. how entered
$("#textbox").rules("add", { regex: "^(\d+(?:(?: \d+)*\/\d+)?)$" })
is because wrapping in string , syntac conflicting? inexperienced @ regex , not sure how go trouble shooting this. please advise me if see error is. thank you.
you need double escape backslashes since you're entering regex string:
$("#textbox").rules("add", { regex: "^(\\d+(?:(?: \\d+)*\/\\d+)?)$" })
or else use regex literal:
$("#textbox").rules("add", { regex: /^(\d+(?:(?: \d+)*\/\d+)?)$/ })
Comments
Post a Comment