你可以自己做一个方法判断输入的是不是字符是不是>='0'和<='9',在insert时效验

解决方案 »

  1.   

    import javax.swing.text.*;
    import java.math.BigDecimal;public class NumberFormatDocument
        extends PlainDocument
    {
        private int textLength = -1;    private int scale = -1;    public NumberFormatDocument()
        {
            init();
        }    public NumberFormatDocument(int len)
        {
            this.textLength = len;
            init();
        }    public NumberFormatDocument(int len, int scale)
        {
            if(len > 0)
                this.textLength = len;
            if(scale > 0)
                this.scale = scale;
            init();
        }    public void setLength(int len)
        {
            if(len > 0)
                this.textLength = len;
            init();
        }    public void setScale(int scale)
        {
            if(scale > 0)
                this.scale = scale;
            init();
        }    protected void init()
        {
            try
            {
                if(0 < scale)
                {
                    String n = "0.";
                    for(int i = 0; i < scale; i++)
                    {
                        n += "0";
                    }
                    this.replace(0,this.getLength(),n,null);
                }
                else
                    this.replace(0,this.getLength(),"0",null);
            }
            catch(Exception e)
            {        }
        }    public void insertString(int offs, String str, AttributeSet a)
            throws BadLocationException
        {
            if (str == null || "".equals(str))
            {
                if (this.getLength() == 0)
                {
                    str = "0";
                    if (scale > 0)
                    {
                        str += ".";
                        for (int i = 0; i < scale; i++)
                        {
                            str += "0";
                        }
                    }
                    insertNumber(offs, str, a);
                }
                return;
            }
            insertNumber(offs, str, a);
        }    private void insertNumber(int offs, String str, AttributeSet a)
            throws BadLocationException
        {
            String text = "";
            int len = 0;
            char strFirstChar = str.charAt(0);
            if (0 == offs)
            {
                if (strFirstChar == '-' || strFirstChar == '+')
                {
                    text = str.substring(1);
                }
                else if (strFirstChar == '.' && scale > 0)
                {
                    str = "0" + str;
                    if (str.length() > 1)
                    {
                        text = str;
                    }
                }
                else
                {
                    text = str;
                }
                len = text.length();
            }
            else
            {
                text = this.getText(0, offs) + str +
                    this.getText(offs, getLength() - offs);
                if (this.getText(0, 1).equals("-") ||
                    this.getText(0, 1).equals("+"))
                {
                    len = text.length() - 1;
                }
                else
                {
                    len = text.length();
                }
            }
            int dot = text.indexOf('.');
            if(dot > 0)
            {
                if ( -1 != textLength && dot > textLength)
                    return;
                if(scale < 0 || (scale > 0 && len - dot - 1 > scale))
                {
                    return;
                }
            }
            else
            {
                if ( -1 != textLength && len > textLength)
                    return;
            }        if (!"".equals(text))
            {
                try
                {
                    new BigDecimal(text);
                }
                catch (Exception e)
                {
                    return;
                }
            }        super.insertString(offs, str, a);
        }
    }
      

  2.   

    使用正则表达式,不过我这没有判断数字的例子,你去google搜一下就有了。
      

  3.   

    不用这么复杂,用java.swing.JFormattedTextField,再配合正则表达式简单的实现类就可以了。
    很简单的,u can do it!
      

  4.   

    <script language="javascript">
    <!--
    function check(){
    if(isNaN(DNo)){
         document.form.DNo.focus();
         alert("工号必须是数字,请重输!");
         return false;
       }
    }
    //-->
    </script>
    <form method="post" onsubmit="return check()" name="form" >
    <table>
    <ta><input type="text" name="DNo"></td>
    </table>
    </form>
      

  5.   

    public static boolean isValid(String s)
      {
        try
        {
          Integer.parseInt(s);
          return true;
        }
        catch(NumberFormatException e)
        {
          return false;
        }
      }