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
};
console.log(email)just before thevar eln..line, does it work? Can you show us what the actual output ofconsole.log(email)is?emailcoming from? I meangetEle('email', clas)[0].valueis fetching from a user input?.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..