如何验证在我的输入字段上正常工作

时间:2014-08-18 11:07:48

标签: javascript html validation

我已经设法让我的字段正确计算,但现在我正在尝试为它们添加一些验证,这证明是困难的。我的目的是确保字段必须只包含数字,并且必须填写。

function audience_details()
if(fields.length === 0) {
     var aud1, aud2, total;
     var aud1 = parseInt(document.getElementById("audience_1_field").value);
     var aud2 = parseInt(document.getElementById("audience_2_field").value);
     var reach = parseInt(document.getElementById("reach_field").value);
     var total = (aud1 + aud2 - reach ) / aud1 * 100 ;
     total_field.value =  parseFloat(total).toFixed(2) + '%';
     estimate.value =  parseFloat(total).toFixed(2) + '%';
}
else {
     //alert fields must be complete / contain numbers only!
}

这里的任何帮助都很可爱http://jsfiddle.net/pjf2v7b8/

2 个答案:

答案 0 :(得分:1)

在这里,我将给出部分解决方案。查看javascript验证和演示小提琴。

function audience_details()
{
var temp = document.getElementById("audience_1_field").value;
var temp1 = document.getElementById("audience_2_field").value;
var temp2 = document.getElementById("reach_field").value;

if(temp == "" || temp == null)
{
    alert("please enter Audience 1 value");
}
else if(temp1 == "" || temp1 == null)
{
    alert("please enter Audience 2 value");
}
else if(temp2 == "" || temp2 == null)
{
    alert("please enter Total Reach value");
}
else
{
 var aud1, aud2, total;
 var aud1 = parseInt(document.getElementById("audience_1_field").value);
 var aud2 = parseInt(document.getElementById("audience_2_field").value);
 var reach = parseInt(document.getElementById("reach_field").value);
 var total = (((aud1 + aud2) - reach ) / aud1) * 100 ;  

  total_field =  parseFloat(total).toFixed(2) + '%';

  estimate =  parseFloat(total).toFixed(2) + '%';

  document.getElementById("total_field").value = total_field;
}   

}

SAMPLE DEMO

正如我上面提到的partial solution,因为我没有在我的解决方案中添加数值验证。目前它将检查他们是否输入值。如果不是,它将通过警报消息。你试着自己进行数值验证。

答案 1 :(得分:1)

请尝试这个答案:

function audience_details(){
     var aud1, aud2, total;
     if(document.getElementById("audience_1_field").value == "" || !onlyNos(document.getElementById("audience_1_field"))){
            alert("Please enter valid number in audience 1");
     }
     else if(document.getElementById("audience_2_field").value == "" || !onlyNos(document.getElementById("audience_2_field"))){

            alert("Please enter valid number in audience 2");
     }
     else if(document.getElementById("reach_field").value == "" || !onlyNos(document.getElementById("reach_field"))){
            alert("Please enter valid number in reache field");
     }
     else{
        var aud1 = parseInt(document.getElementById("audience_1_field").value);
        var aud2 = parseInt(document.getElementById("audience_2_field").value);
        var reach = parseInt(document.getElementById("reach_field").value);
        var total = (aud1 + aud2 - reach ) / aud1 * 100 ;
        total_field.value =  parseFloat(total).toFixed(2) + '%';
     }

}

    function onlyNos(e) {
        try {
            if (e) {
                var charCode = e.value;

                if (charCode.charCodeAt() > 31 && (charCode.charCodeAt() < 48 || charCode.charCodeAt() > 57)) 
                    return false;
                else { return true; }
            }
            else{return false;}
        }
        catch (err) {
            alert(err.Description);
        }
    }