• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1083
  • Last Modified:

Email validation, Javascript: Do not allow email address to start with "@" symbol

as the title says.   I can get it to require an @ symbol, but I can't get it to forbid a email starting with "@"
<html>
 
<head>
 
<script type="text/javascript">
function formReset()
{
document.getElementById("myForm").reset();
}
</script>
 
	<SCRIPT language=Javascript>
     
      function isNumberKey(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;
 
         return true;
      }
      
	</SCRIPT>
 
<script type="text/javascript">
function validate()
{
 
var fname=document.getElementById("firstname").value;
var lname=document.getElementById("lastname").value;
var age=document.getElementById("age").value;
var phone=document.getElementById("phone").value;
var address=document.getElementById("address").value;
var city=document.getElementById("city").value;
var postal=document.getElementById("postal").value;
var at=document.getElementById("email").value.indexOf("@");
 
submitOK="true"
 
if (fname==0)
   {
   alert("You must enter a value for name");
   submitOk="false";
   }
if (lname==0)
   {
   alert("you must enter a value for last name");
   submitOk="false";
   }
if (phone.length<10)
   {
   alert("you must enter a phone number, with only 10 numbers");
   submitOk="false";
   }
if (address==0)
   {
   alert("you must enter an address");
   submitOk="false";
   }
if (city==0)
   {
   alert("you must enter a city");
   submitOk="false";
   }
if (postal.length<5)
   {
   alert("you must enter a postal code");
   submitOk="false";
   }
if (isNaN(age) ||age<1||age>100)
   {
   alert("The age must be a number between 1 and 100");
   submitOK="false";
   }
if (at==-1)
   {
   alert("not a valid email!");
   submitOk="false";
   }
if (submitOK=="false")
   {
   return false;
   }
}
</script>
 
</head>
 
<body>
 
 
<form id="myForm"">
 
<table align="center" border="0" cellpadding="2" cellspacing="8">
 
<tr>
<td align="center">First Name* (max 10 characters):
</td>
<td><input maxlength="10" type="text" id="firstname" />
</td>
</tr>
 
<tr>
<td align="center">Last Name* (max 10 characters):
</td>
<td><input maxlength="10" type="text" name="lastname" id="lastname" />
</td>
</tr>
 
<tr><td align="center">Age* (from 1 to 100):
</td>
<td><input maxlength="3" type="text" name="age" id="age" onkeypress="return isNumberKey(event)" />
</td>
</tr>
 
<tr>
<td align="center">Gender:
</td>
<td>
 
<input type="radio" name="sex" value="male"/>Male
<br>
<input type="radio"
name="sex" value="female"/>Female
 
</td>
</tr>
 
 
<tr><td align="center"><label for="selected">You selected:</label></td><td><input maxlength="10" type="text" name="selectgender" id="selectgender" /></td></tr>
 
<tr><td align="center"><label for="interests">Your Interests:</label></td>
<td>
 
 
 
 
 
 
 
 
 
 
<input type="checkbox" name="vehicle" value="reading" />Reading
<br />
 
<input type="checkbox" name="vehicle" value="sports" />Sports
<br />
 
<input type="checkbox" name="vehicle" value="business" />Business
<br />
 
<input type="checkbox" name="vehicle" value="music" />Music
<br />
 
<input type="checkbox" name="vehicle" value="surfing" />Surfing Web
<br />
 
<input type="checkbox" name="vehicle" value="studies" />Higher Studies
 </td>
 
</tr>
 
<tr>
<td>
E-Mail*:
</td>
<td>
<input type="text" name="email" id="email" />
</td>
</tr>
 
 
<tr>
<td>
Phone*:
</td>
 
<td>
<input type="text" maxlength="10" name="phone" id="phone" onkeypress="return isNumberKey(event)" />
</td>
</tr>
 
 
<tr>
<td>
Address*:
</td>
<td>
<input type="text" name="address" id="address" />
</td>
</tr>
 
<tr>
<td>
City*:
</td>
<td>
<input type="text" name="state" id="city" />
</tr>
 
<tr>
<td>
Postal Code*:
</td>
<td>
<input type="text" name="postal" maxlength="5" id="postal" onkeypress="return isNumberKey(event)" />
</td>
</tr>
 
 
 
 
<tr>
<td>Select State</td>
 
<td>
<select name="state" size="1">
	<option value="AK">AK</option>
	<option value="AL">AL</option>
	<option value="AR">AR</option>
	<option value="AZ">AZ</option>
	<option value="CA">CA</option>
	<option value="CO">CO</option>
	<option value="CT">CT</option>
	<option value="DC">DC</option>
	<option value="DE">DE</option>
	<option value="FL">FL</option>
	<option value="GA">GA</option>
	<option value="HI">HI</option>
	<option value="IA">IA</option>
	<option value="ID">ID</option>
	<option value="IL">IL</option>
	<option value="IN">IN</option>
	<option value="KS">KS</option>
	<option value="KY">KY</option>
	<option value="LA">LA</option>
	<option value="MA">MA</option>
	<option value="MD">MD</option>
	<option value="ME">ME</option>
	<option value="MI">MI</option>
	<option value="MN">MN</option>
	<option value="MO">MO</option>
	<option value="MS">MS</option>
	<option value="MT">MT</option>
	<option value="NC">NC</option>
	<option value="ND">ND</option>
	<option value="NE">NE</option>
	<option value="NH">NH</option>
	<option value="NJ">NJ</option>
	<option value="NM">NM</option>
	<option value="NV">NV</option>
	<option value="NY">NY</option>
	<option value="OH">OH</option>
	<option value="OK">OK</option>
	<option value="OR">OR</option>
	<option value="PA">PA</option>
	<option value="RI">RI</option>
	<option value="SC">SC</option>
	<option value="SD">SD</option>
	<option value="TN">TN</option>
	<option value="TX">TX</option>
	<option value="UT">UT</option>
	<option value="VA">VA</option>
	<option value="VT">VT</option>
	<option value="WA">WA</option>
	<option value="WI">WI</option>
	<option value="WV">WV</option>
	<option value="WY">WY</option>
</select>
 
</td></tr>
 
 
<tr>
<td align="center" colspan="2">
<input type="button" value="Submit" onclick="return validate()" />&nbsp;<input type="reset" onclick="formReset()" value="Reset"/>
</td>
</tr>
 
 
 
</table>
 
</form>
 
</body>
 
</html>

Open in new window

0
shad0wsay3r
Asked:
shad0wsay3r
1 Solution
 
mosidiotCommented:
also add a

if (at==0)
   {
   alert("not a valid email!");
   submitOk="false";
   }

this means that your at is at the first position.. which is the first character...

concurrently you could even change it to

if (at==-1|| at==0)
   {
   alert("not a valid email!");
   submitOk="false";
   }

0
 
HonorGodCommented:
Why not just?

if ( at < 1 ) {
  alert("not a valid email!");
  submitOk="false";
}
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now