简体   繁体   English

正则表达式仅允许5位数字或字母字符后跟5位数字

[英]Regular expression to allow only 5 digit number or alpha character followed by 5 digit number

I want to create validation on a form field which checks that input is a valid ID. 我想在表单字段上创建验证,该字段检查输入是否为有效ID。

Most of the IDs are simple 5 digit numbers, but there a few odd variations where the number could have a leading alpha character (an F or G so eg F12345 ) or ending in an alpha character (an A or B so eg 12345B ). 大多数ID是简单的5位数字,但是有一些奇怪的变化,其中数字可以具有前导α字符(F或G,例如F12345 )或以字母字符结尾(A或B,例如12345B )。

I have the regexp for a 5 digit number but I don't know where to go to allow F/G at the beginning or A/B at the end 我有一个5位数的正则表达式,但我不知道从哪里开始允许F / G或最后A / B

Any ideas? 有任何想法吗?

This regex should do: 这个正则表达式应该做:

/^([FG]?\d{5}|\d{5}[AB])$/

You can use .test() function of RegExp object to validate the string. 您可以使用RegExp对象的.test()函数来验证字符串。

/^([FG]?\d{5}|\d{5}[AB])$/.test("F12345")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM