doushouhe7072 2016-09-02 09:14
浏览 16
已采纳

如何在提交表单之前检查2个输入字段的计算答案

I have a register form with 2 fields includes random int and a result input field for the answer there's a Javascript code to check fields are not empty when submitting I'm trying also to calculate the 2 fields and compare it with the result value

Here's the HTML :

<form method="post" id="contactForm" enctype="multipart/form-data" name="q_sign">
<input type="text" name="q_name" id="senderName"/>
<input type="text" name="q_mail" id="senderEmail" />
<input name="val1" type="text" disabled id="val1" value="php random value1" readonly="readonly" />
<input name="val2" type="text" disabled id="val2" value="php random value2" readonly="readonly" />
<input type="text" name="total" id="total" />

<input type="submit" id="sendMessage" name="sendMessage" value="Register" onClick="return check_data(this.form)" />

Javascript part :

function check_data(form) {
var val1 = (document.q_sign.val1.value);
var val2 = (document.q_sign.val2.value);

if(document.q_sign.q_name.value==''){
    alert("please enter your name");
    return false;
}else if(document.q_sign.q_mail.value==''){
    alert("please enter your email");
    return false;
}else if(document.q_sign.total.value!=(val1+val2)){ //Issue is here
    alert("wrong answer");
    return false;
}else{
    return true;
}}
  • 写回答

1条回答 默认 最新

  • doushi7394 2016-09-02 09:33
    关注
    maybe this is your expect below
    
    
    function check_data(form) {
        var val1 = (document.q_sign.val1.value);
        var val2 = (document.q_sign.val2.value);
        if (document.q_sign.total.value != (eval(val1) + eval(val2))) {
            alert("wrong answer");
            return false;
        } else {
            return true;
        }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?