javascript - How to get validation to work properly on my input fields -
this question has answer here:
i have managed fields calculate trying add validation them proving difficult. aim make sure fields have contain numbers , have filled in.
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 complete / contain numbers only! }
any lovely here form http://jsfiddle.net/pjf2v7b8/
please try answer :
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); } }
Comments
Post a Comment