Regular expression for upper case letter only in JavaScript

前端 未结 3 1588
刺人心
刺人心 2021-01-03 02:17

How can I validate a field only with upper case letters which are alphabetic. So, I want to match any word made of A-Z characters only.

3条回答
  •  南方客
    南方客 (楼主)
    2021-01-03 03:03

    Try something like this for the javascript validation:

    if (value.match(/^[A-Z]*$/)) {
        // matches
    } else {
        // doesn't match
    }
    

    And for validation on the server side in php:

    if (preg_match("/^[A-Z]*$/", $value)) {
        // matches
    } else {
        // doesn't match
    }
    

    It's always a good idea to do an additional server side check, since javascript checks can be easily bypassed.

提交回复
热议问题