年龄未验证不正确的年龄细节js验证

时间:2016-02-16 19:56:27

标签: javascript

我编写了一个简单的JS程序来验证某些数据并且它正常工作。

问题是,当您键入年龄大于100时,它会显示警告消息并导致错误div。它应该只打印您的信息不正确。

意思是,如果用户写入12岁时它可以正常工作,但是当用户写入122或任何大于100的事物时,它也会在消息div中显示警报。它应该重置错误div并仅显示您的信息不正确。

这是我的代码:



function formValidations(){
  var name = document.getElementById("name").value;
  var age = document.getElementById("age").value;
  var job_type    = '';
    	
  if(name==''){
    alert('Name required');
  }
    	
  if(isNaN(age)||age<1||age>100){
    alert('Enter valid age');
  }
    	
  if(!document.getElementById("undergraduate").checked && !document.getElementById("graduate").checked){
    alert('Select Qualification');
  }
    	
  if((name=='') && (isNaN(age)||age<1||age>100) && (!document.getElementById("undergraduate").checked || !document.getElementById("graduate").checked)){
    document.getElementById("Info").innerHTML = 'Please correct all details first';
  }
  else if((name!='') && (isNaN(age)||age>1||age<100) && (document.getElementById("undergraduate").checked || document.getElementById("graduate").checked)){
    document.getElementById("Info").innerHTML = 'Hello <b>'+name+'<b><br>';
    document.getElementById("Info").innerHTML += 'Your age is '+age+' Years<br>';
   if(document.getElementById("undergraduate").checked){
    document.getElementById("Info").innerHTML += 'Your Qualification is '+document.getElementById("undergraduate").value+'<br>';
   }
   else if(document.getElementById("graduate").checked){
    document.getElementById("Info").innerHTML += 'Your Qualification is '+document.getElementById("graduate").value+'<br>';
   }
   if(document.getElementById("it").checked){
    job_type += 'Job1 : '+document.getElementById("it").value+'<br>';		
   }
   else{
    job_type += 'Job1 : None<br>';
   }
   if(document.getElementById("marketing").checked){
    job_type += 'Job2 : '+document.getElementById("marketing").value+'<br>';	
   }
   else{
    job_type += 'Job2 : None<br>';
   }
   if(document.getElementById("academics").checked){
    job_type += 'Job3 : '+document.getElementById("academics").value+'<br>';	
   }
   else{
    job_type += 'Job3 : None<br>';
   }
   document.getElementById("Info").innerHTML += 'Your selected Job Types are <br>'+job_type+'<br>';
  }
 }
    	
    
&#13;
<html>
  <head>
    <title>Pre Assesment Task 2</title>
  </head>
  <body>
    <h1> Online Job Portal </h1>
    <form>
      <table>
        <tr>
          <td>Name:</td>
          <td><input type="text" name="Name" size="20" id="name"></td>
        </tr>
        <tr>
          <td>Age:</td>
          <td><input type="text" name="Age" size="20" id="age"></td>
        </tr>
        <tr>
          <td>Qualification:</td>
          <td>Under Graduate:
            <input type="radio" name="qualification"  Value="Undergraduate" id="undergraduate">
            Graduate:
            <input type="radio" name="qualification" Value="Graduate" id="graduate"></td>
        </tr>
        <tr>
          <td>Select a Job Type:</td>
          <td>IT:
            <input type="checkbox" name="jobtype"  Value="IT" id="it">
            Marketing:
            <input type="checkbox" name="jobtype" Value="Marketing" id="marketing">
            Academics:
            <input type="checkbox" name="jobtype" Value="Academics" id="academics"></td>
        </tr>
        <tr>
          <td colspan="2"><div id="Info"></div></td>
        </tr>
        <tr>
          <td><input type="button" name="Show Information" value="Show Information" onClick="formValidations()"></td>
          <td><input type="reset" name="Reset Form" value="Reset Form"></td>
        </tr>
      </table>
    </form>
  </body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

您应该替换此行

else if((name!='') && (isNaN(age)||age>1||age<100) && (document.getElementById("undergraduate").checked || document.getElementById("graduate").checked))

使用此行

else if((name!='') && (!isNaN(age) && age>1 && age<100) && (document.getElementById("undergraduate").checked || document.getElementById("graduate").checked))

我想这就是你可能正在寻找的东西。