How to check for alphanumeric characters

前端 未结 5 1461
南笙
南笙 2020-12-08 22:58

I\'m writing a custom method for a jQuery plugin:

jQuery.validator.addMethod(\"alphanumeric\", function(value, element) {
        return this.optional(elemen         


        
5条回答
  •  生来不讨喜
    2020-12-08 23:23

    You can use regexes in JavaScript:

    if( yourstring.match(/^[a-zA-Z0-9]+/) ) {
         return true
    }
    

    Note that I used + instead of *. With * it would return true if the string was empty

提交回复
热议问题