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 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名