Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript thread: trouble with function


Message #1 by "Raghuram Kalletla" <raghu@m...> on Tue, 12 Dec 2000 18:25:25 -0600
This is a multi-part message in MIME format.

------=_NextPart_000_0011_01C06468.E565BC00
Content-Type: multipart/alternative;
	boundary="----=_NextPart_001_0012_01C06468.E565BC00"


------=_NextPart_001_0012_01C06468.E565BC00
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

I wrote a function to validate all entries in an HTML form. Two of the 
fields in the form are e-mail address related so the function first 
checks to see if the fields have been entered. If they have been 
entered, it will check to see if there are any invalid or missing 
characters in the e-mail address. It will alert the user if any invalid 
characters are found or if some valid characters are missing. If the 
user does not enter a subject, it will enter "no subject" in the text 
box as the subject. If the message area is empty, it will prompt the 
user to enter a brief message. Here's the problem - It only checks to 
see if two text boxes have been entered (the "TO" and "FROM"). It fails 
to catch the invalid and/or missing characters and also fails to alert 
the user when s/he does not type anything in the message area.

Earlier, I did not have the function to validate the e-mail address and 
if the user did not enter a subject, it alerted the user and forced 
him/her to enter a subject. It worked fine back then. Only after I 
modified it, it started misbehaving. I'm attaching the code (the whole 
html document) as well as pasting the code here. Any help is 
appreciated.



<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
   <TITLE>saturn.uta.edu Web-based email interface.</TITLE>

<SCRIPT LANGUAGE=3D"JavaScript">

function validEmail(email)  // Email validation
{
 invalidChars =3D " /:,;"

 for (i=3D0; i<invalidChars.length; i++)
  {
  badChar =3D invalidChars.charAt(i)
   if (email.indexOf(badchar, 0) > -1) {
    return false
  }
 }
 atPos =3D email.indexOf("@",1)
 if (atPos =3D=3D -1)
  {
  return false
  }
 periodPos =3D email.indexOf(".", atPos)
 if (periodPos =3D=3D -1)
  {
  return false
  }
 if (periodPos+3 > email.length)
  {
  return false
  }
 return true
}

function validForm(frmEmail)    // Form validation
{
 if (frmEmail.TO.value =3D=3D "")
  {
   alert("Please enter your recipient's e-mail address")
   frmEmail.TO.focus()
   return false
  }

 if (frmEmail.FROM.value =3D=3D "")
  {
   alert("Please enter an originating e-mail address. This is usually 
the address where you will read a reply to this message")
   frmEmail.FROM.focus()
   return false
  }

 if (!validEmail(frmEmail.TO.value)) {
   alert("Invalid email address")
   frmEmail.TO.focus()
   frmEmail.TO.select()
   return false
  }

 if (!validEmail(frmEmail.FROM.value)) {
   alert("Invalid email address")
   frmEmail.FROM.focus()
   frmEmail.FROM.select()
   return false
  }

 if (frmEmail.SUBJECT.value =3D=3D "")
  {
   frmEmail.SUBJECT.value =3D "no subject"
  }

 if (frmEmail.MESSAGE.value =3D=3D "")
  {
   alert("Please enter a brief message")
   frmEmail.MESSAGE.focus()
   return false
  }

 return true
}

function confirmReset()   // Reset button click confirmation
{
 if (confirm("Are you sure you want to reset this form? All information 
will be erased!"))
  {
   frmEmail.TO.value =3D=3D ""
   frmEmail.FROM.value =3D=3D ""
   frmEmail.SUBJECT.value =3D=3D ""
   frmEmail.MESSAGE.value =3D=3D ""
   return true
  }
  
 return false
}
</SCRIPT>
</HEAD>

<BODY BACKGROUND =3D "images/back.gif">
   <FORM NAME=3D"frmEmail" ACTION =3D "mail.cgi" METHOD =3D "POST" 
onSubmit=3D"return validForm(this)">
   <CENTER>
   <TABLE BORDER =3D "0" CELLSPACING =3D "0" CELLPADING =3D "0">

      <TR>
         <TD BGCOLOR =3D #DDDDDD WIDTH =3D "10%"><STRONG>
         <FONT FACE =3D "Arial" SIZE =3D "2">To: </FONT>
         </STRONG>
         </TD>
         <TD BGCOLOR =3D #DDDDDD><INPUT NAME =3D "TO" SIZE=3D"25">
         </TD>
      </TR>

      <TR>
         <TD BGCOLOR =3D #DDDDDD>
         <P><FONT FACE =3D Arial SIZE =3D 2>
         <STRONG>From:</STRONG>
         </FONT></P>
         </TD>
         <TD BGCOLOR =3D #DDDDDD><INPUT NAME =3D "FROM" SIZE=3D"25">
         </TD>
      </TR>

      <TR>
         <TD BGCOLOR =3D #DDDDDD>
         <P><FONT FACE =3D Arial SIZE =3D 2>
         <STRONG>Subject:</STRONG>
         </FONT></P>
         </TD>
         <TD BGCOLOR =3D #DDDDDD><INPUT NAME =3D "SUBJECT" SIZE=3D"25">
         </TD>
      </TR>

      <INPUT NAME =3D "MAILSERVER" TYPE=3D"hidden" 
VALUE=3D"nowhere.loopback.edu">

      <TR>
         <TD BGCOLOR =3D #DDDDDD COLSPAN =3D 3>
         <P>
         <STRONG><FONT FACE =3D "Arial" SIZE =3D "2"><BR>Message:
         </FONT>
         </STRONG><BR>
         <TEXTAREA COLS =3D 50 NAME =3D "MESSAGE" ROWS =3D 6
         STYLE =3D "HEIGHT: 200px; WIDTH: 600px"></TEXTAREA>
         </P><P> </P>
         </TD>
      </TR>

      <TR>
 <TD BGCOLOR =3D #DDDDDD COLSPAN =3D 3 ALIGN=3D"center">
        <P>
 <INPUT TYPE=3D"submit" VALUE=3D"Send Message">
   
 <INPUT TYPE=3D"reset" VALUE=3D"Reset Form" onClick=3D"return 
confirmReset()"
        </TD>
     </TR>  

   </TABLE>
   </CENTER>
   </FORM>
</HTML>



Content-Type: text/plain; charset="us-ascii"
Content-description: footer

--- 
NEED TECHNICAL TIPS, TOOLS, AND INSIGHTS?  Is FREE okay?
Visit EarthWeb for the latest in IT Management, Software Development, 
Web Development, Networking & Communications, and Hardware & Systems.  
Click on http://www.earthweb.com for FREE articles, tutorials,
and discussions from the experts.
---
You are currently subscribed to javascript as: $subst('Recip.EmailAddr')
To unsubscribe send a blank email to leave-javascript-$subst('Recip.MemberIDChar')@p2p.wrox.com


------=_NextPart_000_0011_01C06468.E565BC00--

  Return to Index