0

i havea signup form which have a field for email. i made a regular expression to verify it and that regex is working perfectly. that problem i am facing is that the function which does this job is not working. this will become more clear from the following:

var email = getEle('email', clas)[0].value; // 'getEle()' is a function which returns an element whose calss is the first arguement
console.log(email); // logs correct thing which means that email has been caught by the javascript       
function em(){ // this function will check email
        var eln = email.match(/([A-Za-z0-9\._\-]+)@([A-Za-z]+).([A-Za-z]+)/);
        console.log(eln);   
            if(eln.length ===4){
                email = email;
            }else{
                Error = Error + "2) Your Email address is not valid. Please enter valid email Address\n"
            }
    };em();// end

my email validation regex is working properly as you can see here regex101.com
error which is given to me is: Uncaught TypeError: Cannot read property 'length' of null
the first log logs correct thing but the second log logs null
if this is done in console(i am using google chrome) then no error is given.

question is, if no error is given when this thing is done manually, then why error is given to me when the same is done while writing it as a function code.

what is the mistake.

complete code:

var getEle = function(ele, type){
    if(type ==="id"){
    return document.getElementById(ele);
    };
    if(type==="class"){
        return document.getElementsByClassName(ele);
    };
    if(type === 'tag'){
        return docuemnt.getElementsByTagName(ele);
    };
};
function signUpform(){
    var fname = getEle('fname', clas)[0].value,
        lname = getEle('lname', clas)[0].value,
        email = getEle('email', clas)[0].value,
        phoneNo = getEle('phoneNo', clas)[0].value,
        age = getEle('age', clas)[0].value,
        date = getEle('date', clas)[0].value,
        username = getEle('username', clas)[0].value,
        password = getEle('password', clas)[0].value,
        re_pass = getEle('re_pass', clas)[0].value,
        Error = "Errors List: \n";
    console.log(email);
    function flname(){ // this function will check the first name and the last name 
        if(fname === "" || lname ===""){
            Error = Error +'1) You cannot leave first name and last name blank\n';
        }else{
            fname.value = fname.value.replace(/[0-9]+/g,"");
            lname = lname.replace(/[0-9]+/g,"");
        };
    };flname();// end

    function em(){ // this function will check email
        var eln = email.match(/([A-Za-z0-9\._\-]+)@([A-Za-z]+).([A-Za-z]+)/);
        console.log(eln);   
            if(eln.length ===4){
                email = email;
            }else{
                Error = Error + "2) Your Email address is not valid. Please enter valid email Address\n"
            }
    };em();// end 

};
5
  • 2
    HTML5 & jQuery is fine? Commented Jan 28, 2015 at 6:06
  • If you put console.log(email) just before the var eln.. line, does it work? Can you show us what the actual output of console.log(email) is? Commented Jan 28, 2015 at 6:12
  • Where is the email coming from? I mean getEle('email', clas)[0].value is fetching from a user input? Commented Jan 28, 2015 at 6:12
  • Also your regex isn't quite right. In your first character class the backslashes preceeding the . and the - are unnecessary and you're missing a backslash before the . near the end. Also domain names can definitely have dashes and things in them, as well as subdomains, but that's getting somewhat irrelevant since comprehensively covering valid emails with regex is kind of difficult.. Commented Jan 28, 2015 at 6:16
  • everything is working fine and correct. Commented Jan 28, 2015 at 14:58

1 Answer 1

1

No need to check length of matched array, just simplify your em function like this:

function em(){ // this function will check email
    var eln = /\b[\w.-]+@[A-Za-z]+\.[A-Za-z]+\b/.test( email );
    if (!eln)
       Error = Error + "2) Your Email address is not valid. Please enter valid email";
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.