使用javascript比较多个输入

时间:2017-04-23 17:53:26

标签: javascript html

我想要的是以下几点:在我的情况下,我需要日,月和年来匹配SSN字段中写的那些。所有数据都是用户输入。到目前为止,我的代码看起来像这样:

function SSNcheck(){
          if (document.getElementById('SSN').value == "" ||
		      document.getElementById('SSN').value.length !== 13){
			  alert('Invalid SSN: Make sure you have 13 characters');
			  return false;
		  }
}

function Daycheck(){
	      if (document.getElementById('Day').value.length !== 2 || 
		      document.getElementById('Day').value == ""){
			 alert('Invalid Day: Make sure the day is similar with the one from SSN');
			 return false;
		  }
}

function Monthcheck(){
	      if (document.getElementById('Month').value.length !== 2 || 
		      document.getElementById('Month').value == ""){
			 alert('Invalid Month: Make sure the month is similar with the one from SSN');
			 return false;
		  }
}

function Yearcheck(){
	      if (document.getElementById('Year').value.length !== 4 || 
		      document.getElementById('Year').value == ""){
			 alert('Invalid Year: Make sure the year is similar with the one from SSN');
			 return false;
		  }
}
<body>
   <h1>JavaScript Homework</h1>
  <div id="SSN box">
    <form method="post" 
	      onsubmit="SSNcheck() || Daycheck() || Monthcheck() || Yearcheck()" >
    Please insert your SSN here: 
	     <input type="textbox" 
	            id="SSN" 
				placeholder="Must contain 13 characters" 
				onchange="SSNcheck"
				maxlength="13"
				size="19"
				onkeyup="this.value=this.value.replace(/[^\d]/,'')"/> <br/>
	<p></p>
	Day: <input type="number" 
	            id="Day"
				onchange="Daycheck"
				maxlength="2"
				size="2"
				onkeyup="this.value=this.value.replace(/[^\d]/,'')"
				min="1" max="31"
				"/>
	Month: <input type="number" 
	              id="Month"
				  onchange="Monthcheck"
				  maxlength="2"
				  size="2"
				  onkeyup="this.value=this.value.replace(/[^\d]/,'')"
				  min="1" max="12"/>
	Year: <input type="number" 
	             id="Year"
				 onchange="Yearcheck"
				 maxlength="4"
				 size="4"
				 onkeyup="this.value=this.value.replace(/[^\d]/,'')"
				 min="1" max="2016"/>
	<input type="submit" value="Verify">
	</form>
  </div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

我认为这就是你要找的东西?

你的html代码中还有一个“很多”

function SSNcheck(){
          if (document.getElementById('SSN').value == "" ||
		      document.getElementById('SSN').value.length !== 8){
			  alert('Invalid SSN: Make sure you have 13 characters');
			  return false;
		  }
}

function Daycheck(){
	      if (document.getElementById('Day').value.length !== 2 || 
		      document.getElementById('Day').value == ""){
			 alert('Invalid Day: Make sure the day is similar with the one from SSN');
			 return false;
		  }
}

function Monthcheck(){
	      if (document.getElementById('Month').value.length !== 2 || 
		      document.getElementById('Month').value == ""){
			 alert('Invalid Month: Make sure the month is similar with the one from SSN');
			 return false;
		  }
}

function Yearcheck(){
	      if (document.getElementById('Year').value.length !== 4 || 
		      document.getElementById('Year').value == ""){
			 alert('Invalid Year: Make sure the year is similar with the one from SSN');
			 return false;
		  }
}

function Compare(){
  var day = document.getElementById('day');
  var month = document.getElementById('month');
  var year = document.getElementById('year');
  var result1 = document.getElementById('result');
  var ssn = document.getElementById('ssn');
  
  var date = day.value + month.value + year.value ;
  
    if (date == ssn.value){
    			 alert('SSN is correct');
			 return false;
  }else{
    			 alert('SSN is incorrect');
			 return false;
  }
  

};
<h1>JavaScript Homework</h1>
  <div id="SSN box">
    <form method="post" 
	      onsubmit="SSNcheck() || Daycheck() || Monthcheck() || Yearcheck()" >
    Please insert your SSN here: 
	     <input type="textbox" 
	            id="SSN" 
				placeholder="Must contain 13 characters" 
				onchange="SSNcheck"
				maxlength="13"
				size="19"
				onkeyup="this.value=this.value.replace(/[^\d]/,'')"/> <br/>
	<p></p>
	Day: <input type="number" 
	            id="Day"
				onchange="Daycheck"
				maxlength="2"
				size="2"
				onkeyup="this.value=this.value.replace(/[^\d]/,'')
				min="1" max="31"
				"/>
	Month: <input type="number" 
	              id="Month"
				  onchange="Monthcheck"
				  maxlength="2"
				  size="2"
				  onkeyup="this.value=this.value.replace(/[^\d]/,'')"
				  min="1" max="12"/>
	Year: <input type="number" 
	             id="Year"
				 onchange="Yearcheck"
				 maxlength="4"
				 size="4"
				 onkeyup="this.value=this.value.replace(/[^\d]/,'')"
				 min="1" max="2016"/>
	<input type="submit" onclick="Compare()" value="Verify">
	</form>
  </div>