开发者

jQuery Regular Expression Problem

Iam using one Jquery Library which takes the Regular expression in json format in on file and tries to validate that from file

"Studentnumber":{
  regex: "/^[-/@#&$*\w\s]+$/",
  alertText: "* Invalid Student 开发者_如何学CNumber"
}, // ...

I can use this in my text box field as follows

<input type="text" class="validate[required,custom[Studentnumber]] Textbox">

Earlier I used to have JavaScript Regular Expression for the field such as follows

<input type="text" validate="regex" regex='^[-/@#&$*\w\s]+$' name="sno" id="sno">

When I use the same regular expression that is used in the JavaScript for jquery. It is not validating as the same in JavaScript and giving inappropriate results. What is the Difference between the two?


Since your using backspaces in a string, and want them afterwards, you need to escape them, like this:

"Studentnumber":{
  regex: "/^[-/@#&$*\\w\\s]+$/",
  alertText: "* Invalid Student Number"
}

Though, I'm not 100% sure after looking at this, it might be 4 backslashes instead of 2, but the reasoning for why escapes me at the moment.


It depends on how the plugin works with regexp, try removing the quotes or construct a new instance:

"regex": /^[-/@#&$*\w\s]+$/

or

"regex": new RegExp("/^[-/@#&$*\w\s]+$/")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜