I want to check if in a email textbox form field, email has correct syntax, but for Mobile Browsers, in desktop I used below, this will work foe Mobile Devices too?
or better check this on server side(PHP)...(check second snippet)?

function checkFormtemplate()  // javascript used success desktop browsers 
{ 
		
		if (document.getElementById("email-login").value == "") { 
			alert("Error: Username(email) cannot be blank!"); 
			document.getElementById("email-login").focus(); 
			return false; 
		} 
		var retemplate = /^[a-zA-Z0-9]+([-_\.]?[a-zA-Z0-9])+@[a-zA-Z0-9]+([-_\.]?[a-zA-Z0-9])+\.[a-zA-Z\.]{2,4}/  
		var str=document.getElementById("email-login").value;
		if (!retemplate.test(str)) {   //  
			alert("Error: Username must contain only letters, numbers, dash, dot, underscores(first part) and the 'at' sign!");    
			document.getElementById("email-login").focus(); 
			return false; 
		} 
..........................................
..........................................
// php used success
..........................................
if (!eregi("^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$",$_POST['email'])) {   //  
     $message.="Error: Username must contain only letters, numbers, dash, dot, underscores(first part) and the 'at' sign!\n";    // @
     $booleanValue=false; 
  } 
..........................................

I'm not sure if this really work. Is an email supposed to start with a dot? Also, there are other format which is a valid email would not pass this regexp...

1) .@abc.com --> shouldn't it suppose to fail? (obviously, no email name contains only '.')
2) [email]abc@-.com[/email] --> shouldn't it suppose to fail? (obviously, no domain name with only '-')
3) abc@def.ghi.tw --> shouldn't it supposed to pass? (there are valid domain names with more than 1 dot)

It is very tricky when you try to parse a valid email address. There is always a chance that your parser will be wrong because it keeps changing with no real format. By the way, the short hand format for [A-Za-z0-9_] is [\w] if I remember correctly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.