2017-04-23 12 views
1

我想要的是以下幾件事情:在我的情況下,我需要一天,一個月和一年來匹配那些在SSN領域寫的。所有的數據都是用戶輸入。我的代碼看起來像這樣至今:比較多個inputes againts一個用javascript

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

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

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

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

+0

你是什麼意思,‘匹配那些寫在SSN場’呢?SSN僅僅是一個13位數字 – FrankerZ

+0

你有沒有想過先把天,月,年加在一起,所以它看起來像01012001,然後編寫一個代碼來比較它與ssn? – Salman

+0

在這種情況下,SSN代表社會安全號或個人身份證,我不允許把它們放在一起,用戶需要把他的SSN放入並以此爲基礎在他需要再次驗證並提交後,它與「密碼和驗證密碼」類似。 –

回答

0

我認爲這是你在找什麼?

也有「許多在HTML代碼中

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

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

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

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

 
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){ 
 
    \t \t \t alert('SSN is correct'); 
 
\t \t \t return false; 
 
    }else{ 
 
    \t \t \t alert('SSN is incorrect'); 
 
\t \t \t return false; 
 
    } 
 
    
 

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

+0

是的,這確實是我想要的代碼。我拿走了「我的html代碼並且像以前一樣改變了一切,唯一的問題是,儘管我輸入了值,或者如果不在代碼中說明SSN是正確的,它只會檢查它們是否具有長度相同,但這是一個很大的幫助!我會嘗試解決它,我可能需要在某處有一個「.indexOf」 –

+0

@SiposStelian我已經更改了我的代碼,現在它可以正常工作 – Salman

+0

您確實解決了這個問題但現在我的SSN是正確的,只有當我所有的字段都是空白的,否則無論我插入什麼樣的數字組合,然後我重複它,它說SSN是不正確的。另外,如果我的SSN將是1820315498231我的日期編號將位置5-6(15),3-4(03)的月份和0-2的年份(182代表1882年),讓事情變得更加困難,無論如何,謝謝你的時間,你真的幫了我很多 –