NameSilo

Email form validation javascript

Namecheap AuctionsNamecheap Auctions
SpaceshipSpaceship
SpaceshipSpaceship
Watch

Brian2011

Established Member
Impact
1
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.

If the user does not enter a correctly formatted email address, a pop up alert box appears to let them know.
 
Last edited:
0
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
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.
 
0
Close
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 :)
 
0
Close
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._
[email protected]
.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
 
0
Close
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
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:
0
Close
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 [email protected] still get thru so if you need more robust solution check http://www.regular-expressions.info/email.html for fast examples :)
 
0
Close
DomainEasy: white label portfolios are live. Enable yours.

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Switch between accounts
You can now stay logged in to more than one NamePros account in the same browser. Click your avatar, then "Accounts in this browser", to...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back