繁体   English   中英

HTML / Java-如何将电话输入字段的前3个数字限制为特定数字?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM