Regular expression which allows numbers, spaces, plus sign, hyphen and brackets

前端 未结 1 841
轮回少年
轮回少年 2020-12-28 16:59

I am using jquery validator where I have added a method to validate a string which allow only numbers, spaces, plus sign, hyphen and brackets. Number is mandatory in the str

相关标签:
1条回答
  • 2020-12-28 17:15

    You can add the required characters into character class as

    /^(?=.*[0-9])[- +()0-9]+$/
    

    Regex Demo

    Regex Explanation

    • (?=.*[0-9]) postive look ahead. Ensures that there is atleast one digit

    • [- +()0-9]+ matches numbers, spaces, plus sign, hyphen and brackets

    OR

    If you are reluctant in using look aheads. You could write without them a lenghier regex as

    /^[- +()]*[0-9][- +()0-9]*$/
    
    0 讨论(0)
提交回复
热议问题