NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page email form validation javascript

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search
6 members in live chat ~  


Reply
 
LinkBack Thread Tools
Old 12-17-2011, 07:36 AM THREAD STARTER               #1 (permalink)
New Member
 
Brian2011's Avatar
Join Date: Dec 2011
Location: Leeds, UK
Posts: 11
Brian2011 is an unknown quantity at this point
 



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.
Brian2011 is offline   Reply With Quote
Old 12-28-2011, 04:38 PM   #2 (permalink)
Senior Member
 
nasaboy007's Avatar
Join Date: Jul 2005
Location: NJ
Posts: 1,219
nasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud of
 



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.
nasaboy007 is offline   Reply With Quote
Old 01-11-2012, 02:42 AM   #3 (permalink)
NamePros Member
Join Date: Jan 2012
Posts: 59
Mark Talks will become famous soon enough
 



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
Mark Talks is offline   Reply With Quote
Old 01-12-2012, 12:15 PM   #4 (permalink)
4pm
NamePros Regular
 
4pm's Avatar
Join Date: Jun 2011
Location: Moscow
Posts: 626
4pm is a splendid one to behold4pm is a splendid one to behold4pm is a splendid one to behold4pm is a splendid one to behold4pm is a splendid one to behold4pm is a splendid one to behold4pm is a splendid one to behold
 



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
__________________
Webbox | 1st.ly | PR checker
4pm is offline   Reply With Quote
Old 01-12-2012, 03:03 PM THREAD STARTER               #5 (permalink)
New Member
 
Brian2011's Avatar
Join Date: Dec 2011
Location: Leeds, UK
Posts: 11
Brian2011 is an unknown quantity at this point
 



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.
Brian2011 is offline   Reply With Quote
Old 01-12-2012, 05:14 PM   #6 (permalink)
4pm
NamePros Regular
 
4pm's Avatar
Join Date: Jun 2011
Location: Moscow
Posts: 626
4pm is a splendid one to behold4pm is a splendid one to behold4pm is a splendid one to behold4pm is a splendid one to behold4pm is a splendid one to behold4pm is a splendid one to behold4pm is a splendid one to behold
 



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 
}
the function returns false if the string does not look like email address and true otherwise.
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
__________________
Webbox | 1st.ly | PR checker
4pm is offline   Reply With Quote
Reply


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

 
All times are GMT -7. The time now is 02:50 PM.

Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger