最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Regular expression to allow only 5 digit number or alpha character followed by 5 digit number - Stack Overflow

programmeradmin4浏览0评论

I want to create validation on a form field which checks that input is a valid 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).

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

Any ideas?

I want to create validation on a form field which checks that input is a valid 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).

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

Any ideas?

Share Improve this question asked Jul 23, 2012 at 11:18 TomTom 13k50 gold badges153 silver badges247 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

This regex should do:

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

You can use .test() function of RegExp object to validate the string.

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论