tags:

views:

323

answers:

2

Hello.I am allowing a string to contain only alphabets and underscore,but is i enter fist character as alphabet or underscore and later if i put any invalid character then this validation is being done.I have done validation as follows:

function permission_validate()
{var permission=document.permissionForm.permission.value;var allowedStr=/[A-Za-z_]/;

if(!allowedStr.test(permission)){document.getElementById("permission_Er").innerHTML="* Required field can contain Only A-Z/az/_";

document.permissionForm.permission.focus();return false;}else{return true;}
+2  A: 

The correct regex to use would be

/^[a-zA-Z\_]+$/g

^ matches beginning
$ matches end
g matches the whole string.

Johannes Jensen
Don't think you need backslash before underscore. Underscore is not a resreved character in regex
Marco Demajo
I know, I just always use backslashes behind characters that aren't letters to be on the safe side. :P
Johannes Jensen
+2  A: 
/^[a-z_]+$/gi

Note: You need anchors ^ and $ and + to accept one of more characters of alphabets and underscores.

S.Mark