Validation of text fields and email fields using regular expression
I want to validate my text fields and the text fields should not allow
<< < > ( ) { } [ ] ? & * ~ ` ! # $ % ^ = + | \ : ' " , ; any of the above special characters and can contain - _ @ / . these special characters. How can i do it? The same thing applies to my email field also. Can any one help me out with this?
Thanks in 开发者_StackOverflowadvance Ravikiran
^(?("")("".+?""@)|((0-9a-zA-Z)(?<=[0-9a-zA-Z])@))(?([)([(\d{1,3}.){3}\d{1,3}])|(([0-9a-zA-Z][-\w][0-9a-zA-Z].)+[a-zA-Z]{2,6}))$
this is a good one (taken off Microsoft's site)
MSDN site
I've enclosed all the special characters (plus escaped all of them) you don't want in your fields in this expression,
\[\<\>\(\)\{\}\[\]\?\&\*\~\`\!\#\$\%\^\=\+\|\\\:\'\"\,\;]\
I think you can do validation like this, try to match this expression on each of your fields, if it finds a match, that field is invalid, else it's valid as it doesn't contain any of the special characters.
i suggest to use javascript for this with following code.
Code:
function isSpclChar(){
var iChars = "!#$%^&*()+=[]\\\';,{}|\":<>?";
for (var i = 0; i < document.qfrm.q.value.length; i++) {
if (iChars.indexOf(document.qfrm.q.value.charAt(i)) != -1) {
alert ("The box has special characters. \nThese are not allowed.\n");
return false;
}
}
}
精彩评论