javascript regex for special characters

前端 未结 12 812
囚心锁ツ
囚心锁ツ 2020-11-29 22:28

I\'m trying to create a validation for a password field which allows only the a-zA-Z0-9 characters and .!@#$%^&*()_+-=

I can\'t seem to

12条回答
  •  醉话见心
    2020-11-29 22:46

    function nameInput(limitField)
    {
    //LimitFile here is a text input and this function is passed to the text 
    onInput
    var inputString = limitField.value;
    // here we capture all illegal chars by adding a ^ inside the class,
    // And overwrite them with "".
    var newStr = inputString.replace(/[^a-zA-Z-\-\']/g, "");
    limitField.value = newStr;
    }
    

    This function only allows alphabets, both lower case and upper case and - and ' characters. May help you build yours.

提交回复
热议问题