check a textbox for invalid characters using js and regular expressions

后端 未结 3 1849
滥情空心
滥情空心 2020-12-21 08:25

I am having a hard time figuring out how RegExp work.

I need to rewrite some ASP code into html and js, and I\'ve hit an obstacle in this part:



        
3条回答
  •  失恋的感觉
    2020-12-21 08:45

    [^#%&*:<>?/{|}]+ looks like a valid expression to me (although typically regular expressions are enclosed in forward-slashes). It's basically checking to see of the filename contains any of the illegal characters within the square brackets (apart from the caret ^ which indicates negation).

    function regexValidator(control) {
            var val = $(control).val();
            if(val == undefined || val == '') {
    
                $(control).attr("class", "invalid");
            } 
            else if(val.match(/[^#%&*:<>?/{|}]+/)) {
                // Valid
            }
            else {
                // Invalid
            }
        }
    

提交回复
热议问题