简体   繁体   English

用户输入验证Java类

[英]user input validation java classes

This is my validation for user ID number, when testing, I entered an invalid string with a length less than 10 and it sets the input and doesn't execute the else statement. 这是我对用户ID号的验证,在测试时,我输入了长度小于10的无效字符串,它设置了输入并且不执行else语句。

The Source Code: 源代码:

private String phoneNum;

public personalInfo(String phNum) {
    setPhoneNum(phNum);
}

public String getPhoneNum() {
    return phoneNum;
}

public void setPhoneNum(String phNum) {
    if (phoneNum.startsWith("05")&&(phoneNum.length()==10)){
        phoneNum = phNum;
    }
    else throw new IllegalArgumentException ("Invalid Phone Number!");
}

your method looks like this... 你的方法看起来像这样...

public void setPhoneNum(String phNum) {
    if (phoneNum.startsWith("05")&&(phoneNum.length()==10)){
        phoneNum = phNum;
    }

and there you are not validating the parameter, but the variable phoneNum ... 并且您没有验证参数,而是变量phoneNum ...

do instead: 改为:

public void setPhoneNum(String phNum) {
    if (phNum.startsWith("05")&&(phNum.length()==10)){
        phoneNum = phNum;
    }

You should be validating the value you that you are getting ( phNum ) and set it to the member variable phoneNum . 您应该验证要获取的值( phNum ),并将其设置为成员变量phoneNum

if (phNum.startsWith("05")&&(phNum.length()==10)){
    phoneNum = phNum;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM