Java 强制在 Jtextfield 中包含数字和字符的代码
Java code to enforce to have number and character in Jtextfield
我有 Jtextfield,我想对其进行格式化,以便它接受以下格式。
KYC123456L
输入总是以 "KYC" 开头,以 "L" 结尾,中间有 6 个数字。
UI 有一个按钮,可以复制其他组件的内容并将其保存到文本文件中。但在它复制之前,它应该验证 jtextfield 并复制如果只有上面的格式匹配,如果不是要显示的消息。
请推荐。
构建正则表达式:^KYC[0-9]{6}L$
以匹配您问题中所述的模式。
它将匹配以 KYC 开头,然后是 6 位数字,最后以 L 结尾的字符串。
注意:^
标记字符串的开头,而 $
标记字符串的结尾。
String patternString = "^KYC[0-9]{6}L$";
Pattern p = Pattern.compile(patternString);
String test = jTextField.getText();
Matcher m = p.matcher(test);
boolean matches = m.matches();
if(matches == true)
// allow
else
// JOptionpane.showMessageDialog ---> your desired error message.
我有 Jtextfield,我想对其进行格式化,以便它接受以下格式。
KYC123456L
输入总是以 "KYC" 开头,以 "L" 结尾,中间有 6 个数字。
UI 有一个按钮,可以复制其他组件的内容并将其保存到文本文件中。但在它复制之前,它应该验证 jtextfield 并复制如果只有上面的格式匹配,如果不是要显示的消息。
请推荐。
构建正则表达式:^KYC[0-9]{6}L$
以匹配您问题中所述的模式。
它将匹配以 KYC 开头,然后是 6 位数字,最后以 L 结尾的字符串。
注意:^
标记字符串的开头,而 $
标记字符串的结尾。
String patternString = "^KYC[0-9]{6}L$";
Pattern p = Pattern.compile(patternString);
String test = jTextField.getText();
Matcher m = p.matcher(test);
boolean matches = m.matches();
if(matches == true)
// allow
else
// JOptionpane.showMessageDialog ---> your desired error message.