简体   繁体   中英

Validating E-Mail address using JavaScript or JQuery

I have to validate E-Mail address using either JS or JQ. Right now I am using JS but unable to pass the value of the text box as the parameter for JS. I want this to be implemented onchange. I found solutions only using a button to validate which i don't want to.

Here is the HTML code.

            <div class="form-group">
                <label class="control-label">Father's E-Mail Address</label>
                <input maxlength="30" pattern=".{1,50}"  onchange="validateEmail(document.getElementById('txtFatherEmail').value);" title="Input Invalid"  type="text" required="required" class="form-control" placeholder="Enter Father's E-Mail Address" id="txtFatherEmail" runat="server"/>
            </div>

Here is the JS I have used.

    function validateEmail(email) {               
        var emailReg = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
        var valid = emailReg.test(email);

        if (!valid) {
            alert("False");
        } else {
            alert("True");
        }
    }

Also I would like to know if there's any better way to do this.

If I understand correctly you want to validate input as you type.
To do this you can use onkeyup event.

<div class="form-group">
    <label class="control-label">Father's E-Mail Address</label>
    <input maxlength="30" pattern=".{1,50}"  onkeyup="validateEmail(this.value);" title="Input Invalid"  type="text" required="required" class="form-control" placeholder="Enter Father's E-Mail Address" id="txtFatherEmail" runat="server"/>
</div>

Why are you using getElementById with "value"? Shouldn't you be using thee jquery syntax? Remember a jquery element is not a javasript don element. Maybe that's the trick...

                        function isvalid(x){
                            regexp1 = /@/g
                            gmail = /gmail.com/g
                            hotmail = /hotmail.com/g
                            if(regexp1.test(x) == true){
                                if(x.match(regexp1).length == 1){
                                    x = x.split("@")
                                    if(gmail.test(x[1]) == true){
                                        if(x[1].match(gmail).length == 1 && x[1].length == 9){
                                            alert("ok valid")

                                        }else{
                                            alert("not valid")

                                        }
                                    }else if(hotmail.test(x[1]) == true){
                                        if(x[1].match(hotmail).length == 1 && x[1].length == 11){
                                            alert("ok valid")
                                        }else{
                                            alert("not valid")
                                        }
                                    }else{
                                            alert("no mail")
                                    }
                                }else{
                                    alert("too much @")
                                }
                            }else{
                                alert("no @")
                            }
                        }

this function is the jquery email check function and it just looks for gmail and hotmail and if you want just to check email from hot and gmail it is the reliable function for it

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