简体   繁体   中英

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'

How can I do this with a JavaScript function? 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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