Several very useful JavaScript functions

Source: Internet
Author: User
Tags comments functions mail reset
javascript| function Reset () resets the contents of each field after pressing the reset button.



The function submitforms () sends an e-mail message after checking the legality of the field after pressing the Submit button.



The function Isname () checks for the legality of the last name section.



function Isemail () checks the e-mail address field for legality.



The function IsBrowser () compares the browser field with the automatically detected browser version.



function Iscountry () checks the legality of the Country field.



The function iscomment () Checks the opinion field for legality and does not allow null values.



The function Isfavorite () checks for the legality of the favorite site field and does not allow null values.



The program also provides some tips, such as how to determine the browser version, the operation of strings, and so on.



The result is provided to you in the form of an email with various fields entered by the guest. The program is relatively long, but not ugly, the following is the source code:





<HTML>

<HEAD>

<TITLE> use JavaScript to compile guest book program </TITLE>

<scriptlanguage= "JavaScript" >

<!--Begin

Someone@abc.com is your own e-mail address.

Varemailaddress= "Someone@abc.com";

Functiontoname ()

{

Vartonameval=document.forms[0].elements[1].value;

Tonameval= "Mailto:someone@abc.com?subject=guestbookexample";

This.document.mail.action=tonameval;

}

Functionreset () {

Document.forms[0].elements[0].value= "";

Document.forms[0].elements[1].value= "";

Document.forms[0].elements[2].value=

Navigator.appname+ "" +navigator.appversion;

Document.forms[0].elements[3].value= "";

Document.forms[0].elements[4].value= "";

Document.forms[0].elements[5].value= "";

Document.forms[0].elements[0].focus ();

}

Functionsubmitforms () {

if ((Isname ()) && (Isemail ()) && (IsBrowser ())

&& (Iscountry ()) && (Iscomment ()) && (Isfavorite ())

if (Confirm ("\nyou ' Reabouttoe-mailtheform.\n\nclick

Onyestosubmit.\n\nclickonnotoabort. "))

{

Alert ("\nyoursubmissionwillnowbemadeto:

\ n \ +emailaddress+ "\n\n\nthankyou!");

Returntrue;

}

Else

{

Alert ("\nyouhavechosentoabortthesubmission.");

Returnfalse;

}

Else

Returnfalse;

}

Functionisname () {

Varstr=document.forms[0].elements[0].value;

if (str== "") {

Alert ("\nthenamefieldisblank.\n\npleaseenteryourname.")

Document.forms[0].elements[0].focus ();

Returnfalse;

}

for (vari=0;i<str.length;i++)

{

Varch=str.substring (i,i+1);

if ((ch< "a" | |) Z "<ch" && (ch< "A" | |) Z "<ch)" &&ch!= ")

{

Alert ("\nthenamefieldonlyacceptsletters

&spaces.\n\npleasere-enteryourname. ");

Document.forms[0].elements[0].select ();

Document.forms[0].elements[0].focus ();

Returnfalse;

}

}

Returntrue;

}

Functionisemail ()

{

Emailaddress=document.forms[0].elements[1].value;

if (document.forms[0].elements[1].value== "") {

Alert ("\nthee-mailfieldisblank.

\n\npleaseenteryoure-mailaddress. ")

Document.forms[0].elements[1].focus ();

Returnfalse;

}

if (Document.forms[0].elements[1].value.indexof (' @ ', 0) ==-1| |

Document.forms[0].elements[1].value.indexof ('. ', 0) ==-1)

{

Alert ("\nthee-mailfieldrequiresa\" @\)

Anda\ ". \" Beused.\n\npleasere-enteryoure-mailaddress. ")

Document.forms[0].elements[1].select ();

Document.forms[0].elements[1].focus ();

Returnfalse;

}

Else

{

ToName ();

Returntrue;

}

}

Functionisbrowser ()

{

if (document.forms[0].elements[2].value!

=navigator.appname+ "" +navigator.appversion)

{

if (Confirm ("\nyou ' Vechangedyourbrowser

Type.\n\nclickonoktokeepchanges.\

N\nclickoncanceltorestoredetectedbrowser. "))

Returntrue

Else

{

Document.forms[0].elements[2].value=

Navigator.appname+ "" +navigator.appversion;

Returntrue;

}

}

Else

Returntrue;

}

Functioniscountry () {

Varstr=document.forms[0].elements[3].value;

if (str== "") {

Alert ("\nthecountryfieldis

Blank.\n\npleaseenteryourcountry. ")

Document.forms[0].elements[3].focus ();

Returnfalse;

}

for (vari=0;i<str.length;i++) {

Varch=str.substring (i,i+1);

if ((ch< "a" | |) Z "<ch" &&

(ch< "A" | |) Z "<ch)" &&ch!= ")

{

Alert ("\nthecountryfieldonlyaccepts

Letters&spaces.\n\npleasere-enteryourcountry. ");

Document.forms[0].elements[3].select ();

Document.forms[0].elements[3].focus ();

Returnfalse;

}

}

Returntrue;

}

Functioniscomment () {

if (document.forms[0].elements[4].value== "") {

if (Confirm ("\nyou ' Reabouttosubmit

Withoutleavingacomment.\n\nclick

Oncanceltoincludeacomment.\n\nclick

Onoktocontinuewithoutacomment. "))

Returntrue

Else

{

Document.forms[0].elements[4].focus ();

Returnfalse;

}

}

Else

Returntrue

}

Functionisfavorite () {

if (document.forms[0].elements[5].value== "") {

if (Confirm ("\nyou ' reabouttosubmitwithout

Listingyourfavoritesites.\n\nclickoncancel

Toincludefavorites.\n\nclickonoktocontinue

Withoutlistingfavorites. "))

Returntrue

Else

{

Document.forms[0].elements[5].focus ();

Returnfalse;

}

}

Else

Returntrue

}

End-->

</SCRIPT>

</HEAD>



<BODY>

<CENTER>

<formenctype= "Text/plain"

Name= "Mail" method= ' get '

action= ' mailto:someone@abc.com '

>

<TABLEBORDER=0WIDTH=400>

<TR>

<tdalign= "Center" ><FONTCOLOR=800000>

<STRONG>Enteryourname:</STRONG></FONT></TD>

<tdalign= "Center" ><FONTCOLOR=800000>

<STRONG>Enteryoure-mailaddress:</STRONG>

</FONT></TD>

</TR>

<TR>

<tdalign= "Center" ><inputtype= "text"

Name= "Name" size=30maxlength=40></td>

</TD>

<tdalign= "Center" ><inputtype= "text"

Name= "Email" size=30maxlength=40></td>

</TR>

<TR>

<tdalign= "Center" ><FONTCOLOR=800000>

<STRONG>Yourbrowser</STRONG></FONT></TD>

<tdalign= "Center" ><FONTCOLOR=800000>

<STRONG>Enteryourcountry:</STRONG></FONT></TD>

</TR>

<TR>

<tdalign= "Center" ><inputtype= "text"

Name= "Browser" size=30maxlength=60></td>

<tdalign= "Center" ><inputtype= "text"

Name= "Country" size=30maxlength=60>

</TD>

</TR>

</TABLE>

<CENTER>

<FONTCOLOR=800000><STRONG>

Leaveacommentorsuggestion:</strong></font><br>

<textareaname= "Comments"

Rows=5cols=50wrap=yes>comments?

Suggestions?</textarea><p><p>

<FONTCOLOR=800000><STRONG>

Listyourfavoritesites:</strong></font><br>

<tdalign= "Center" ><textareaname= "Favorites"

Rows=5cols=50wrap=yes>

Anysitesishouldtakealookat?</textarea>

<P><P>

<inputtype= "Submit" value= "Submit" >

<center><inputtype= "reset" value= "reset"

>

</FORM>

</CENTER>

</BODY>

</HTML>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.