| | |||||
| ||||||||
| Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics. |
![]() |
| | LinkBack | Thread Tools |
| | THREAD STARTER #1 (permalink) |
| New Member Join Date: Dec 2011 Location: Leeds, UK
Posts: 11
![]() | email form validation javascript This code will validate an email address to ensure the user enters an email address in the correct format: <script Language="Javascript"> <!-- function checkform(theform){ var EmailOk = true var Temp = theform.email var AtSym = Temp.value.indexOf('@') var Period = Temp.value.lastIndexOf('.') var Space = Temp.value.indexOf(' ') var Length = Temp.value.length - 1 if ((AtSym < 1) || (Period <= AtSym+1) || (Period == Length ) || (Space != -1)){ EmailOk = false alert('Please enter a valid e-mail address!') theform.email.focus(); return (false);} } //--> </script> Example form code: <form method=" POST" action="/success.htm" onsubmit="return checkform(this)" name="myform"> When this form is submitted, the javascript runs and will only go to '/success.htm' if user enters a correctly formatted email address. ????: NamePros.com http://www.namepros.com/programming/740733-email-form-validation-javascript.html If the user does not enter a correctly formatted email address, a pop up alert box appears to let them know.
Last edited by Brian2011; 12-18-2011 at 06:07 AM.
|
| | |
| | #2 (permalink) |
| Senior Member Join Date: Jul 2005 Location: NJ
Posts: 1,219
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | Keep in mind that client-side validation is just for the sake of user experience and in no way should be depended on. Everything should be revalidated on the server side as well.
__________________ Hacksar.com - Your source for random computer tips and tricks! MySiteMemberships.com - Keep track of your site registration information! Like my post? Rep is appreciated! |
| | |
| | #3 (permalink) |
| NamePros Member Join Date: Jan 2012
Posts: 59
![]() | A nice little script and I can think of several uses where it would be handy in full or in part, such as free forum/site software that don't allow for open source editing and/or PHP support. But, with users being able to disable JS and/or manipulate it easier, it's definitely got it's risk. Nice little script though |
| | |
| | #4 (permalink) |
| NamePros Regular Join Date: Jun 2011 Location: Moscow
Posts: 626
![]() ![]() ![]() ![]() ![]() ![]() ![]() | sorry to say but this script checks very few things.. for example it will let thru the following bogus emails as valid ones: a@b@c._ ....@...x .xyz *&^%$#@@@@(.)>> and so on.. you'll also screw the users that input empty space before or after (happen often when copy-pasting) valid email and will be presented with error message.. i would google "javascript email validation" for good ready to use reliable regex example (there are many to choose from) - correct email validation in javacript (as well as in php) could not be implemented effectively without regex imo |
| | |
| | THREAD STARTER #5 (permalink) |
| New Member Join Date: Dec 2011 Location: Leeds, UK
Posts: 11
![]() | email form validation javascript Yeah, the script does have some drawbacks as you mention but it is a good starting point and weeds out most email address input errors. Of course it doesn't work for those who are determined to enter a wrong email address. I agree that regex would be able to check input to a finer degree but understanding how regex works can be a little daunting for some. Here's another little bit of code for ASP programmers that will convert text to proper case as there is no built-in function in VBScript to do this: <% ''''''' FUNCTION TO CAPITALIZE THE FIRST CHARACTER OF EACH WORD IN THE INPUT STRING ''''''''' Function PCase(strInput) iPosition = 1 Do While InStr(iPosition, strInput, " ", 1) <> 0 iSpace = InStr(iPosition, strInput, " ", 1) strOutput = strOutput & UCase(Mid(strInput, iPosition, 1)) strOutput = strOutput & LCase(Mid(strInput, iPosition + 1, iSpace - iPosition)) iPosition = iSpace + 1 Loop strOutput = strOutput & UCase(Mid(strInput, iPosition, 1)) StrOutput = strOutput & LCase(Mid(strInput, iPosition + 1)) PCase = strOutput ????: NamePros.com http://www.namepros.com/showthread.php?t=740733 End Function '''''''''''''''''''''''''''''''' END FUNCTION ''''''''''''''''''''''''''''''''''''''''''''''' %> ...so 'peter Smith' or 'PeTER smiTH' becomes 'Peter Smith' <% ' example Str = "PeTER smiTH" Str = PCase(Str) Response.write(Str) %> Hope this is useful to someone :-)
Last edited by Brian2011; 01-12-2012 at 03:09 PM.
|
| | |
| | #6 (permalink) |
| NamePros Regular Join Date: Jun 2011 Location: Moscow
Posts: 626
![]() ![]() ![]() ![]() ![]() ![]() ![]() | basic user-side errors in emails can be handled more accurately in this way: Code: function checkEmail(s) {
s = s.replace(/^\s+/, '').replace(/\s+$/, ''); // trim spaces from both ends
if (!s.length) return !1; // exit early if empty
var r =/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i; // simple regex, works 99% of time
return r.test(s); // test & return
} yet the strings like ...@...abcd still get thru so if you need more robust solution check http://www.regular-expressions.info/email.html for fast examples |
| | |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| {Website Marketing} - Targeted Traffic | Backlinks | List Building | Email Marketing | EconomicalDomains | Advertising & SEO Services | 0 | 11-08-2011 07:01 PM |
| Catch-all Whois Email Address: Good or Bad Idea? | Ms Domainer | Domain Name Discussion | 5 | 10-23-2011 08:10 PM |
| How do I save email content to a text file? | FPForum | Programming | 0 | 09-29-2011 05:05 PM |
| Simple paypal form required | Ziggery.com | Web Development Wanted | 5 | 08-03-2011 05:33 PM |
| Scam CraigsList Email says from CEO Sandra Clark | DnPresident | Warnings & Alerts | 1 | 05-18-2011 06:47 PM |