java 文本框不能输入_文本框输入限制

本文介绍了如何使用Java正则表达式限制文本框的输入,如只允许输入字母、汉字、数字等,并提供了相关的正则表达式实例,如限制小数点后最多两位、去除字符串重复字符等,适用于表单验证和字符串处理。
摘要由CSDN通过智能技术生成

3只能输入字母和汉字

4只能输入英文字母和数字,不能输入中文

5只能输入数字和英文chun

6小数点后只能有最多两位(数字,中文都可输入),不能输入字母和运算符号:

7小数点后只能有最多两位(数字,字母,中文都可输入),可以输入运算符号

附:

正则表达式用于字符串处理,表单验证等场合,实用高效,但用到时总是不太把握,以致往往要上网查一番。我将一些常用的表达式收藏在这里,作备忘之用。本贴随时会更新。

匹配中文字符的正则表达式: [\u4e00-\u9fa5]

匹配双字节字符(包括汉字在内):[^\x00-\xff]

应用:计算字符串的长度(一个双字节字符长度计2,ASCII字符计1)

String.prototype.len=function(){return this.replace([^\x00-\xff]/g,"aa").length;}

匹配空行的正则表达式:\n[\s| ]*\r

匹配HTML标记的正则表达式:/.*|/

匹配首尾空格的正则表达式:(^\s*)|(\s*$)

应用:javascript中没有像vbscript那样的trim函数,我们就可以利用这个表达式来实现,如下:

String.prototype.trim = function()

{

return this.replace(/(^\s*)|(\s*$)/g, "");

}

利用正则表达式分解和转换IP地址:

下面是利用正则表达式匹配IP地址,并将IP地址转

Java 中,可以使用 JTextField 组件来实现文本框输入限制。下面是一些限制的例子: 1. 限制文本框只能输入数字 ```java JTextField textField = new JTextField(); textField.setDocument(new NumberOnlyDocument()); ``` 其中 NumberOnlyDocument 是一个自定义的 Document 类,代码如下: ```java import javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; import javax.swing.text.Document; import javax.swing.text.DocumentFilter; public class NumberOnlyDocument extends DocumentFilter { @Override public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException { if (string == null) { return; } String newValue; Document doc = fb.getDocument(); StringBuilder sb = new StringBuilder(); sb.append(doc.getText(0, doc.getLength())); sb.insert(offset, string); newValue = sb.toString(); if (newValue.matches("\\d*")) { super.insertString(fb, offset, string, attr); } } @Override public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException { if (text == null) { return; } String newValue; Document doc = fb.getDocument(); StringBuilder sb = new StringBuilder(); sb.append(doc.getText(0, doc.getLength())); sb.replace(offset, offset + length, text); newValue = sb.toString(); if (newValue.matches("\\d*")) { super.replace(fb, offset, length, text, attrs); } } } ``` 2. 限制文本框只能输入特定长度的字符串 ```java JTextField textField = new JTextField(10); textField.setDocument(new LengthLimitedDocument(10)); ``` 其中 LengthLimitedDocument 是一个自定义的 Document 类,代码如下: ```java import javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; import javax.swing.text.PlainDocument; public class LengthLimitedDocument extends PlainDocument { private int limit; public LengthLimitedDocument(int limit) { this.limit = limit; } @Override public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { if (getLength() + str.length() <= limit) { super.insertString(offs, str, a); } } @Override public void replace(int offs, int len, String str, AttributeSet a) throws BadLocationException { if (getLength() - len + str.length() <= limit) { super.replace(offs, len, str, a); } } } ``` 这些代码可以在文本框中实现输入限制
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值