Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Email Validation in javascript 1

Status
Not open for further replies.

rhmb

IS-IT--Management
Apr 9, 2009
11
GB
Hello,
I am very new to this so please excuse me if i do not use the correct terminology.
I have a function in my form that validates fields which does work.
However i want to add a function that will make sure that the email address field actually has an email address in it, not just any old characters.
I have found various bits of code on the internet but everytime i try to add another function to validate the email address none of the functions work.
Please see below my script.
This is from the HTML:
<form id="Litreqform" action="testlitreq.asp" method="post" name="Litreqform" onsubmit="return Litreqform_Validator(this)" style="z-index: 1; width: 955px; height: 672px; position: absolute; top: 863px; left: 20px">
This is from the Javascript:
<script language="javascript">
function Litreqform_Validator(theForm)

{

if (theForm.First_Name.value == "")
{
alert("Please enter a value for the \"First_Name\" field.");
theForm.First_Name.focus();
return (false);
}

if (theForm.Last_Name.value == "")
{
alert("Please enter a value for the \"Last_Name\" field.");
theForm.Last_Name.focus();
return (false);
}

if (theForm.Company_Name.value == "")
{
alert("Please enter a value for the \"Company_Name\" field.");
theForm.Company_Name.focus();
return (false);
}

if (theForm.Title.value == "")
{
alert("Please enter a value for the \"Title\" field.");
theForm.Title.focus();
return (false);
}

if (theForm.Address1.value == "")
{
alert("Please enter a value for the \"Address1\" field.");
theForm.Address1.focus();
return (false);
}

if (theForm.City.value == "")
{
alert("Please enter a value for the \"City\" field.");
theForm.City.focus();
return (false);
}

if (theForm.Postcode_Zip.value == "")
{
alert("Please enter a value for the \"Postcode/Zip\" field.");
theForm.Postcode_Zip.focus();
return (false);
}

if (theForm.Country.value == "")
{
alert("Please enter a value for the \"Country\" field.");
theForm.Country.focus();
return (false);
}

if (theForm.Phone.value == "")
{
alert("Please enter a value for the \"Phone\" field.");
theForm.Phone.focus();
return (false);
}

if (theForm.Fax.value == "")
{
alert("Please enter a value for the \"Fax\" field.");
theForm.Fax.focus();
return (false);
}

if (theForm.Email.value == "")
{
alert("Please enter a value for the \"Email\" field.");
theForm.Email.focus();
return (false);
}


return (true);
}

</script>

 
function Litreqform_Validator(theForm)
[red]
function isValidEmail(myEmail)
dim isValidE
dim regEx

isValidE = True
set regEx = New RegExp

regEx.IgnoreCase = False

regEx.Pattern = "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"
isValidE = regEx.Test(myEmail)

isValidEmail = isValidE
End Function
[/red]
{

if (theForm.First_Name.value == "")
{
alert("Please enter a value for the \"First_Name\" field.");
theForm.First_Name.focus();
return (false);
}

if (theForm.Last_Name.value == "")
{
alert("Please enter a value for the \"Last_Name\" field.");
theForm.Last_Name.focus();
return (false);
}

if (theForm.Company_Name.value == "")
{
alert("Please enter a value for the \"Company_Name\" field.");
theForm.Company_Name.focus();
return (false);
}

if (theForm.Title.value == "")
{
alert("Please enter a value for the \"Title\" field.");
theForm.Title.focus();
return (false);
}

if (theForm.Address1.value == "")
{
alert("Please enter a value for the \"Address1\" field.");
theForm.Address1.focus();
return (false);
}

if (theForm.City.value == "")
{
alert("Please enter a value for the \"City\" field.");
theForm.City.focus();
return (false);
}

if (theForm.Postcode_Zip.value == "")
{
alert("Please enter a value for the \"Postcode/Zip\" field.");
theForm.Postcode_Zip.focus();
return (false);
}

if (theForm.Country.value == "")
{
alert("Please enter a value for the \"Country\" field.");
theForm.Country.focus();
return (false);
}

if (theForm.Phone.value == "")
{
alert("Please enter a value for the \"Phone\" field.");
theForm.Phone.focus();
return (false);
}

if (theForm.Fax.value == "")
{
alert("Please enter a value for the \"Fax\" field.");
theForm.Fax.focus();
return (false);
}

if (theForm.Email.value == "")
{
alert("Please enter a value for the \"Email\" field.");
theForm.Email.focus();
return (false);
}
[red]
if Field = "Email" then
If isValidEmail(myField) = false then
alert("Please enter a valid Email address");
theForm.Email.focus();
return (false);
}
[/red]

return (true);
}

</script>



I have added the extra function at the top and the bit that calls it at the bottom from another page that i know works -
but it does not work here and it stops the other function working.
 
i have done that too and that does not seem to work either!
<script language="javascript">
function Litreqform_Validator(theForm)
[red]
function echeck(str) {

var at="@"
var dot="."
var lat=str.indexOf(at)
var lstr=str.length
var ldot=str.indexOf(dot)
if (str.indexOf(at)==-1){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(at,(lat+1))!=-1){
alert("Invalid E-mail ID")
return false
}

if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(dot,(lat+2))==-1){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(" ")!=-1){
alert("Invalid E-mail ID")
return false
}

return true
}

function ValidateForm(){
var emailID=document.frmSample.txtEmail

if ((email.value==null)||(email.value=="")){
alert("Please Enter your Email ID")
emailID.focus()
return false
}
if (echeck(email.value)==false){
emailID.value=""
emailID.focus()
return false
}
return true

{
[/red]
if (theForm.First_Name.value == "")
{
alert("Please enter a value for the \"First_Name\" field.");
theForm.First_Name.focus();
return (false);
}

if (theForm.Last_Name.value == "")
{
alert("Please enter a value for the \"Last_Name\" field.");
theForm.Last_Name.focus();
return (false);
}

if (theForm.Company_Name.value == "")
{
alert("Please enter a value for the \"Company_Name\" field.");
theForm.Company_Name.focus();
return (false);
}

if (theForm.Title.value == "")
{
alert("Please enter a value for the \"Title\" field.");
theForm.Title.focus();
return (false);
}

if (theForm.Address1.value == "")
{
alert("Please enter a value for the \"Address1\" field.");
theForm.Address1.focus();
return (false);
}

if (theForm.City.value == "")
{
alert("Please enter a value for the \"City\" field.");
theForm.City.focus();
return (false);
}

if (theForm.Postcode_Zip.value == "")
{
alert("Please enter a value for the \"Postcode/Zip\" field.");
theForm.Postcode_Zip.focus();
return (false);
}

if (theForm.Country.value == "")
{
alert("Please enter a value for the \"Country\" field.");
theForm.Country.focus();
return (false);
}

if (theForm.Phone.value == "")
{
alert("Please enter a value for the \"Phone\" field.");
theForm.Phone.focus();
return (false);
}

if (theForm.Fax.value == "")
{
alert("Please enter a value for the \"Fax\" field.");
theForm.Fax.focus();
return (false);
}

if (theForm.Email.value == "")
{
alert("Please enter a value for the \"Email\" field.");
theForm.Email.focus();
return (false);
}


return (true);
}
</script>
 
Hi

Anyway, I rewrote that VBScript function. And also I rewrote the Litreqform_Validator abit, because I found it hard to read.
Code:
function isValidEmail(myEmail)
{
  return /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/.test(myEmail)
}

function Litreqform_Validator(theForm)
{
  var list={
    'First_Name':'First Name',
    'Last_Name':'Last Name',
    'Company_Name':'Company Name',
    'Title':'Title',
    'Address1':'Address 1',
    'City':'City',
    'Postcode_Zip':'Postcode/Zip',
    'Country':'Country',
    'Phone':'Phone',
    'Fax':'Fax',
    'Email':'Email'
  }

  var one

  for (one in list) if (theForm.elements[one].value=='') {
    alert('Please enter a value for the "'+list[one]+'" field.');
    theForm.elements[one].focus();
    return false;
  }

  if (!isValidEmail(theForm.Email.value)) {
    alert('Please enter a valid Email address');
    theForm.Email.focus();
    return (false);
  }

  return true
}

Feherke.
 
Hi

Sorry, but your second try is even worst, so I will not debug it. You placed the echeck function's declaration inside the Litreqform_Validator function and that is wrong. Anyway, that echeck function is old style and personally I do not like it.

Feherke.
 
Thanks so much for your help - i know it must be really dificult to try and explain to someone who does not know what they are talking about!!

Thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top