[英]HTML/Java - How to restrict first 3 number of tel input field to specific number?
我嘗試在wordpress上建立注冊站點,這也需要SMS電話驗證。 問題是我的網站與巴基斯坦有關。 我還想注冊來自巴基斯坦的用戶。 但是SMS網關可以將短信發送到世界各地,因此每個人都可以注冊。 現在我希望當用戶在此字段中輸入電話號碼時:
<input type="tel" name="user_phone" id="user_phone" class="input" value="" size="25">
前三個數字必須為“ 923”。 在輸入字段中預先寫入的內容,用戶無法更改/刪除它們,或者在提交用戶時收到“無效的數字格式,數字必須以932開頭”錯誤。 怎么可能? 您可以在http://goo.gl/SSst3上查看其示例網站,希望您能理解我想要做什么。
php可以做到這一點..運行一個php腳本。 嘗試閱讀我在php上的筆記,他們有很好的例子來幫助您
讀:
https://docs.google.com/file/d/0B-C0T_-acxRqamV4Y1pnUnZtenc/edit?usp=sharing
使用javascript驗證:
在表單上提交驗證以檢查此內容:
var s = $("#phonenumber").val()
if(s.substr(0, 3)) == "923"
{
form.submit();
}
else
{
alert("Invalid number format, number must start with 932");
return false;
}
創建2個輸入字段,一個為只讀字段,另一個為可編輯字段。
<input type="telprefix" name="user_phone" id="user_phone_pref" class="input" value="932" size="5" tabindex="20" readonly="readonly"/>
<input type="tel" name="user_phone" id="user_phone" class="input" value="" size="20" tabindex="25" />
聽起來如何:
function validate(elem)
{
var anum=/(^923(\d){10}$)/; //using this regex after the number 923
// we are allowed to have 10 digits
if(anum.test(elem.value) == true)
{
elem.style.border="2px solid #0F0"; //green light
}
else
{
elem.style.border="2px solid #F00"; //red light
}
}
在html中,我們有:
<input type="text" id="phone" onchange="validate(this)" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.