2013-11-28 38 views
1

我试过在这个正则表达式的不同位置放置一个撇号,但我仍然收到错误。我需要把它放在哪里?如何添加撇号到jQuery验证正则表达式?

$.validator.addMethod("customvalidation", 
     function(value, element) { 
      return (/^[A-Za-z\d=#$%@_\-]+$/.test(value)); 
     }, 
"Sorry, no special characters allowed" 
); 

事情我已经尝试:

//putting the apostraphe before the @ sign without escaping 
     (/^[A-Za-z\d=#$%'@_\-]+$/.test(value)); 

//putting the apostraphe as the last value in the brackets with and without escaping 
     (/^[A-Za-z\d=#$%'@_\- \']+$/.test(value)); 
+0

没有什么特别之处正则表达式撇号。您应该可以将其添加到括号中。 – Barmar

+0

无论我把它放在哪里,我都会收到错误,表示没有允许特殊字符 – Spilot

+0

确保你输入了一个真正的撇号,而不是“智能报价”。 – Barmar

回答

1

我看过你的问题多次,通过所有的意见都没有了。这一评论触击:

console.log(value) gives me the value of whatever is in the input such as:萨德的帖子

你说得对,这将不会成功

/^[A-Za-z\d=#$%'@_-]+$/.test("Sade's Post"); 

问题不是apostrophe却是空白的存在在你的输入字符串中是不包含在你的角色类

改变你的性格类,包括空白也:

[A-Za-z\d\s=#$%'@_-] 

现在试试这个regex.test

result = /^[A-Za-z\d\s=#$%'@_-]+$/.test("Sade's Post"); 
// returns true now