BluwAngel Posted April 1, 2012 Share Posted April 1, 2012 so i have field for email <input id="email" name="email" type="email" /><br /> is there and HTML command to prevent it from leaving blank (if you type "test" it will say its not email. any way to do that if some leave it blank?) i created filter with JS, but i want same that bubble box where it say "please enter email or something like that Quote Link to comment https://forums.phpfreaks.com/topic/260149-input-type-email/ Share on other sites More sharing options...
AyKay47 Posted April 2, 2012 Share Posted April 2, 2012 *pseudo code* if($('#email') == '') alert("blank email"); put this wherever you check the values of the inputs in JS. Quote Link to comment https://forums.phpfreaks.com/topic/260149-input-type-email/#findComment-1333420 Share on other sites More sharing options...
Jessica Posted April 2, 2012 Share Posted April 2, 2012 No, HTML cannot do that. You could use PHP and regular expressions to check if it's an email, then return an error. Considering users can easily turn off JS, this is practically required. Quote Link to comment https://forums.phpfreaks.com/topic/260149-input-type-email/#findComment-1333649 Share on other sites More sharing options...
caliux Posted April 10, 2012 Share Posted April 10, 2012 If you want in PHP you can use if(email != "" ),and i think that in jQuery it is if( $('#email').val() != "" ) Quote Link to comment https://forums.phpfreaks.com/topic/260149-input-type-email/#findComment-1335958 Share on other sites More sharing options...
freaker87 Posted April 10, 2012 Share Posted April 10, 2012 use this... javascript code <script type="text/javascript"> function validateForm() { var x=document.forms["myForm"]["email"].value; var atpos=x.indexOf("@"); var dotpos=x.lastIndexOf("."); if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) { alert("Not a valid e-mail address"); return false; } } </script> this is html code.... <form name="myForm" action="demo_form.asp" onsubmit="return validateForm();" method="post"> Email: <input type="text" name="email"> <input type="submit" value="Submit"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/260149-input-type-email/#findComment-1336004 Share on other sites More sharing options...
AyKay47 Posted April 10, 2012 Share Posted April 10, 2012 you can have some sort of validation client side, but should always also have validation server side, just in case JS is disabled, this will prevent security leaks. as jesirose already stated. Quote Link to comment https://forums.phpfreaks.com/topic/260149-input-type-email/#findComment-1336032 Share on other sites More sharing options...
trq Posted April 10, 2012 Share Posted April 10, 2012 The "email" type attribute will actually validate the data it contains if you set the "required" attribute. Of course, not all browsers yet support this functionality however. Quote Link to comment https://forums.phpfreaks.com/topic/260149-input-type-email/#findComment-1336039 Share on other sites More sharing options...
AyKay47 Posted April 10, 2012 Share Posted April 10, 2012 The "email" type attribute will actually validate the data it contains if you set the "required" attribute. Of course, not all browsers yet support this functionality however. I actually forgot that it validates it's contents if required. Here is the regex that it uses for validation: /^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/ not the tightest regex, but efficient enough. and the w3 docs if anyone cares to see: http://dev.w3.org/html5/markup/input.email.html but yah as noted, not all browsers yet support this and server side validation is still required. Quote Link to comment https://forums.phpfreaks.com/topic/260149-input-type-email/#findComment-1336045 Share on other sites More sharing options...
freaker87 Posted April 10, 2012 Share Posted April 10, 2012 use this in php $email( is a variable contain value of email field) if(!$email == "" && (!strstr($email,"@") || !strstr($email,"."))) { echo "<h2>Use Back - Enter valid e-mail</h2>\n"; $badinput = "<h2> NOT submitted</h2>\n"; echo $badinput; } Quote Link to comment https://forums.phpfreaks.com/topic/260149-input-type-email/#findComment-1336052 Share on other sites More sharing options...
AyKay47 Posted April 10, 2012 Share Posted April 10, 2012 use this in php $email( is a variable contain value of email field) if(!$email == "" && (!strstr($email,"@") || !strstr($email,"."))) { echo "<h2>Use Back - Enter valid e-mail</h2>\n"; $badinput = "<h2> NOT submitted</h2>\n"; echo $badinput; } this is a poor check, just use filter_var with the FILTER_VALIDATE_EMAIL flag. Quote Link to comment https://forums.phpfreaks.com/topic/260149-input-type-email/#findComment-1336057 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.