views:

45

answers:

2

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 Number"
}, // ...

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?

A: 

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]+$/")
David
@david:Thanks a lot, Thanks a lot,Thanks a lot,Thanks a lot,Thanks a lot,Thanks a lot,Thanks a lot,Thanks a lot,Thanks a lot,Thanks a lot.but what is the difference in having the double quotes and not having it in j query
Someone
+1  A: 

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.

Nick Craver