简体   繁体   English

使用javascript的特定电子邮件模式控制

[英]specific e-mail pattern control with javascript

I've a textBox control and would like my users to only be able to enter ....@firm1.com' or '....@firm2.com' 我有一个textBox控件,希望我的用户只能输入....@firm1.com''....@firm2.com'

How can I do this with a JavaScript function? 如何使用JavaScript函数执行此操作? The function must take a textbox value. 该函数必须采用文本框值。

It doesn't make sense to give the user a textbox but allowing him to write two values only, 给用户一个文本框是没有意义的,而只允许他写两个值,
You should use a dropdown instead: 您应该改用下拉菜单:

<select>
    <option value="0"> @firm1.com </option>
    <option value="1"> @firm2.com </option>     
</select>

Update: 更新:

If you really have to use textbox for some unknown reason: 如果由于某些未知原因您确实必须使用文本框:

$('#textboxId').change(function(){
    if (!this.value.match(/(@firm1.com|@firm2.com)$/g))
        alert('invalid value'); 
});​

Live DEMO 现场演示

Try this code- 试试这个代码-

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
    var patt1 = "[A-Za-z0-9._%+-]+@firm(1|2).com";
    function demoShowMatchClick(str) {
        var re = new RegExp(patt1);
        var m = re.exec(str);
        if (m == null) {
            document.getElementById("match").innerHTML = "no match";
        } else {
            document.getElementById("match").innerHTML = "match";
        }
    }
</script>
</head>
<body>
<span id="match"></span>
<input type="text" onkeyup="demoShowMatchClick(this.value)" />
</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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