在文本区域中测试特定的文本格式 [英] Test for a specific text format in a text area
问题描述
我正在写一些javascript来验证文本区域中的用户输入。以下是对输入格式的要求:
- 输入格式必须是以空格分隔的两个十六进制值(即A7 B3 9D) 。
- 输入必须是有效的十六进制值。
regExpression valueOne.match([0-9A-Fa-f] {1})
(或者至少我希望这是推荐的方式)。所以我只想寻找一些关于如何以简单高效的方式处理第一要求的输入。
谢谢!
/ ^ [0]解决方案
-9A-F] {2}(\s [0-9A-F] {2})* $ / i
$
^ //字符串开头
[0-9A-F] b $ b {2} //两个字符0-9或AF
(\s [0-9A-F] {2})* //零个或多个空间实例,后跟
//两个字符0-9或AF
$ //字符串结尾
其中<
使用它:
var valueOne = //在这里设置您的textarea的值
if(/ ^ [0-9A-F] {2}(\s [0 -9A-F] {2})* $ / i.test(valueOne)){
//是好的,做点什么
} else {
//不好,做点什么
}
I am writing some javascript to validate user input in a text area. Here are the requirements for input format:
- Input must be in the format of two hex values separated by a space (i.e. A7 B3 9D).
- Input must be valid hex values.
I have requirement #2 sorted out with a regExpression valueOne.match("[0-9A-Fa-f]{1}")
(or at least I hope that is the recommended way to do it). So I am just looking for some input on how to go about handling requirement number one in a simple and efficient way.
Thanks!
This regex will do it:
/^[0-9A-F]{2}(\s[0-9A-F]{2})*$/i
That is:
^ // beginning of string
[0-9A-F]{2} // two characters of 0-9 or A-F
(\s[0-9A-F]{2})* // zero or more instances of a space followed by
// two characters of 0-9 or A-F
$ // end of string
Where the i
flag at the end makes it case insensitive.
To use it:
var valueOne = // set to your textarea's value here
if (/^[0-9A-F]{2}(\s[0-9A-F]{2})*$/i.test(valueOne)) {
// is OK, do something
} else {
// is not OK, do something
}
这篇关于在文本区域中测试特定的文本格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!