简体   繁体   English

用Java进行Formvalidation

[英]Formvalidation with Javascript

i wanted to validate my form but im stuck with the validation of formfield persnr. 我想验证我的表单,但我坚持使用formfield persnr的验证。 It won´t compare the string. 它不会比较字符串。 For this i already tried the comparison and operators (or || ). 为此,我已经尝试了比较和运算符(或||)。 The validation of the other fields is ok. 其他字段的验证是可以的。 Did i use the operators wrong? 我用错了运算符吗?

function checkForm() {
    var strFehler = '';
    if (document.forms[0].user.value == "user")
        strFehler += "user not ok!\n";
    if (document.forms[0].test.value == "")
        strFehler += "test not ok!\n";
    if (document.forms[0].time.value == "")
        strFehler += "time not ok";
    if (document.forms[0].cost.value == "")
        strFehler += "cost not ok!\n";
    if (document.forms[0].persnr.value != "13088") || (document.forms[0].persnr.value != "10286")
    strFehler += "persnr false!\n";
    if (strFehler.length > 0) {
        alert("problems!!: \n\n" + strFehler);
        return (false);
    }
}

I expected that the validation would show an alert if the value isn´t 13088 or 10286 but no message pops up. 我希望如果该值不是13088或10286,但没有弹出消息,验证将显示警报。

This: 这个:

if (document.forms[0].persnr.value != "13088") || (document.forms[0].persnr.value != "10286")

Needs to be changed to this: 需要更改为:

if ((document.forms[0].persnr.value != "13088") || (document.forms[0].persnr.value != "10286"))

Your are missing parentheses to have both conditions inside the the if statement . 您缺少括号,以使if语句中同时包含两个条件。

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

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