简体   繁体   中英

allow only alphabets and underscore using javascript

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;}

The correct regex to use would be

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

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

/^[a-z_]+$/gi

注意 :您需要使用锚^和$和+来接受字母和下划线的多个字符之一。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM