我參加了一個在線編碼測試,必須對錶單輸入進行數據驗證(請參閱下面的問題描述)。根據問題描述,有人可以告訴我我的方法可能有什麼問題。HTML文檔驗證表單用戶輸入JavaScript
這是我的代碼
function solution() {
try {
var person =$("#type_person").is(":checked");
var company =$("#type_company").is(":checked");
var fName =$("#first_name").val();
var lName =$("#last_name").val();
var email =$("#email").val();
var companyName =$("#company_name").val();
var phone =$("#phone").val();
var result = true;
if (person) {
result = result && fName.match("[a-zA-Z ]+");
result = result && lName.match("[a-zA-Z ]+");
result = result && email.match("^[a-zAZ.]{1,64}@[a-zA-Z.]{1,64}$");
} else if (company) {
result = result && companyName.match("(.)+");
result = result && phone.match("^[\d]{3}-[\d]{3}-[\d]{4}$");
} else {
result = false;
}
} catch (ex) {
// Possible when a DOM Element isn't found
return false;
}
return result;
}
input {
float: left;
}
input[type=text],
input[type=button] {
clear: left;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" id="type_person" name="type" value="person" />
<input type="radio" id="type_company" name="type" value="company" checked/>
<input type="text" id="first_name" name="first_name" value="John" />
<input type="text" id="last_name" name="last_name" value="Doe" />
<input type="text" id="email" name="email" value="[email protected]" />
<input type="text" id="company_name" name="company_name" value="ACME" />
<input type="text" id="phone" name="phone" value="12-3" />
<input type="button" value="Validate" />
問題描述
您將得到一個HTML文檔與表格,你的任務是驗證它在JavaScript。該表單用於輸入聯繫人數據,無論是個人還是公司。名爲「type」的單選按鈕控制着這個選擇。只有一個這樣的按鈕(價值「人」或「公司」)將被檢查。
如果「人」選項被選中,你應該驗證以下字段(只有這些):
FIRST_NAME和姓氏應該是非空。例如,「John」和「」(空格)是有效的,但「」不是。
電子郵件應按照一個簡單的方案進行驗證:它應該只包含一個@字符。此外,兩個部分(@之前和之後)應該由至少一個且最多64個字符組成,其中只包含字母,數字和/或點(a-z,A-Z,0-9,。)。例如,「[email protected]」和「。@」。是有效的,但「@ example.com」不是。
如果「公司」選項被選中,你應該驗證以下字段(只有這些):
COMPANY_NAME應該是非空的;
電話應該只包含數字,破折號( - )和空格。它應該至少有六位數字。例如,「234-567-890」是有效的,但「12-3」不是。
寫一個函數;函數解決方案();
給定一個表示HTML文檔的DOM樹,返回一個布爾值,該值指示本文檔中的表單是否有效。
例如,假設與
標籤中的以下內容的HTML文件:
<input type="radio" id="type_person" name="type" value="person" checked/>
<input type="radio" id="type_company" name="type" value="company"/>
<input type="text" id="first_name" name="first_name" value="John"/>
<input type="text" id="last_name" name="last_name" value="Doe"/>
<input type="text" id="email" name="email" value="[email protected]"/>
<input type="text" id="company_name" name="company_name" value=""/>
<input type="text" id="phone" name="phone" value="234-567-890"/>
</form>
你的函數應返回true,而給出下列內容:
<form>
<input type="radio" id="type_person" name="type" value="person"/>
<input type="radio" id="type_company" name="type" value="company" checked/>
<input type="text" id="first_name" name="first_name" value="John"/>
<input type="text" id="last_name" name="last_name" value="Doe"/>
<input type="text" id="email" name="email" value="[email protected]m"/>
<input type="text" id="company_name" name="company_name" value="ACME"/>
<input type="text" id="phone" name="phone" value="12-3"/>
</form>