Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java字符文本字段验证_Java - Fatal编程技术网

java字符文本字段验证

java字符文本字段验证,java,Java,我需要名称文本字段验证,我使用的是这段代码,但这段代码工作得并不完美 Name_text.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent EVT){ if(EVT.getKeyChar()>='a'&& EVT.getKeyChar()<='z'|| EVT.getKeyChar()>='

我需要名称文本字段验证,我使用的是这段代码,但这段代码工作得并不完美

Name_text.addKeyListener(new KeyAdapter() {

            @Override
          public void keyPressed(KeyEvent EVT){

            if(EVT.getKeyChar()>='a'&& EVT.getKeyChar()<='z'|| EVT.getKeyChar()>='A'&& EVT.getKeyChar()<='Z'|| EVT.getKeyChar()==8|| EVT.getKeyChar()==26){

            Name_text.setEditable(true);
            }
            else{

         JOptionPane.showMessageDialog(null, "enter characters only");

            }
Name\u text.addKeyListener(新的KeyAdapter(){
@凌驾
按下公共无效键(KeyEvent EVT){

如果(EVT.getKeyChar()>='a'&&EVT.getKeyChar()='a'&&EVT.getKeyChar(),我想您应该使用以下代码进行验证:

        if (Character.isDigit(EVT.getKeyChar())) {
                Name_text.setEditable(true);
                Name_text.setText("");
            } else {
                JOptionPane.showMessageDialog(null, "enter characters only");
            }

由您的评论编辑。

您确实应该使用该类执行这种类型的验证,这正是它的设计目的。如果用户将文本粘贴到字段中或您使用
setText
,您将不会通过
KeyListener


您还可以查看一些示例

您的要求是什么???[a-zA-Z]或26或8(keychar)?您正在做什么,错误是什么?解释错误是什么以及您需要它做什么:“工作不完美”是错误的错误描述。我需要name textfield验证,我正在使用此代码,但它无法正常工作。问题是,当我输入数字时,会出现对话框,并且数字也输入到textfield中。我已根据您的代码对其进行了更新。您的代码缺少一些括号。我无法通过documentfilter执行此操作,,,,,可以吗帮助我PLZ您想要什么?当输入数字时,出现对话框,数字不应输入文本字段?我想要两者,当输入数字时,出现对话框,数字不应输入文本字段。