熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

幾個很有用的javascript函數

2013-11-15 11:35:33  來源: JSP教程 
函數Reset()按Reset按鈕後對各字段的內容復位



  函數submitForms()按submit按鈕後對字段合法性檢查後發送電子郵件



  函數isName()對姓名字段進行合法性檢查



  函數isEmail()對電子郵件地址字段進行合法性檢查



  函數isBrowser()對浏覽器字段與自動檢測的浏覽器版本進行比較



  函數isCountry()對國家字段進行合法性檢查



  函數isComment()對意見字段進行合法性檢查不允許為空值



  函數isFavorite()對喜歡的站點字段進行合法性檢查不允許為空值



  程序中還提供了一些技巧例如如何判斷浏覽器的版本字符串的操作等等



  結果是以電子郵件的形式提供給你的裡面有客人輸入的各個字段程序比較長但不難看懂下面是源代碼





<HTML>

<HEAD>

<TITLE>用JavaScript編制留言簿程序</TITLE>

<SCRIPTLANGUAGE=JavaScript>

<!Begin

//是你自己的電子郵件地址

varemailAddress=;

functiontoName()

{

vartoNameval=documentforms[]elements[]value;

toNameval=mailto:?subject=GuestBookexample;

thisdocumentmailaction=toNameval;

}

functionReset(){

documentforms[]elements[]value=;

documentforms[]elements[]value=;

documentforms[]elements[]value=

navigatorappName++navigatorappVersion;

documentforms[]elements[]value=;

documentforms[]elements[]value=;

documentforms[]elements[]value=;

documentforms[]elements[]focus();

}

functionsubmitForms(){

if((isName())&&(isEmail())&&(isBrowser())

&&(isCountry())&&(isComment())&&(isFavorite()))

if(confirm(\nYoureabouttoemailtheform\n\nClick

onYEStosubmit\n\nClickonNOtoabort))

{

alert(\nYoursubmissionwillnowbemadeto:

\n\n+emailAddress+\n\n\nThankyou!);

returntrue;

}

else

{

alert(\nYouhavechosentoabortthesubmission);

returnfalse;

}

else

returnfalse;

}

functionisName(){

varstr=documentforms[]elements[]value;

if(str==){

alert(\nTheNAMEfieldisblank\n\nPleaseenteryourname)

documentforms[]elements[]focus();

returnfalse;

}

for(vari=;i<strlength;i++)

{

varch=strsubstring(ii+);

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

{

alert(\nTheNAMEfieldonlyacceptsletters

&spaces\n\nPleasereenteryourname);

documentforms[]elements[]select();

documentforms[]elements[]focus();

returnfalse;

}

}

returntrue;

}

functionisEmail()

{

emailAddress=documentforms[]elements[]value;

if(documentforms[]elements[]value==){

alert(\nTheEMAILfieldisblank

\n\nPleaseenteryouremailaddress)

documentforms[]elements[]focus();

returnfalse;

}

if(documentforms[]elements[]valueindexOf(@)==||

documentforms[]elements[]valueindexOf()==)

{

alert(\nTheEMAILfieldrequiresa\@\

anda\\beused\n\nPleasereenteryouremailaddress)

documentforms[]elements[]select();

documentforms[]elements[]focus();

returnfalse;

}

else

{

toName();

returntrue;

}

}

functionisBrowser()

{

if(documentforms[]elements[]value!

=navigatorappName++navigatorappVersion)

{

if(confirm(\nYouvechangedyourbrowser

type\n\nClickonOKtokeepchanges\

n\nClickonCanceltorestoredetectedbrowser))

returntrue

else

{

documentforms[]elements[]value=

navigatorappName++navigatorappVersion;

returntrue;

}

}

else

returntrue;

}

functionisCountry(){

varstr=documentforms[]elements[]value;

if(str==){

alert(\nTheCOUNTRYfieldis

blank\n\nPleaseenteryourcountry)

documentforms[]elements[]focus();

returnfalse;

}

for(vari=;i<strlength;i++){

varch=strsubstring(ii+);

if(((ch<a||z<ch)&&

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

{

alert(\nTheCOUNTRYfieldonlyaccepts

letters&spaces\n\nPleasereenteryourcountry);

documentforms[]elements[]select();

documentforms[]elements[]focus();

returnfalse;

}

}

returntrue;

}

functionisComment(){

if(documentforms[]elements[]value==){

if(confirm(\nYoureabouttosubmit

withoutleavingacomment\n\nClick

onCANCELtoincludeacomment\n\nClick

onOKtocontinuewithoutacomment))

returntrue

else

{

documentforms[]elements[]focus();

returnfalse;

}

}

else

returntrue

}

functionisFavorite(){

if(documentforms[]elements[]value==){

if(confirm(\nYoureabouttosubmitwithout

listingyourfavoritesites\n\nClickonCANCEL

toincludefavorites\n\nClickonOKtocontinue

withoutlistingfavorites))

returntrue

else

{

documentforms[]elements[]focus();

returnfalse;

}

}

else

returntrue

}

//End>

</SCRIPT>

</HEAD>



<BODY>

<CENTER>

<FORMENCTYPE=text/plain

NAME=mailMETHOD=GET

ACTION=mailto:

onSubmit=returnsubmitForms()>

<TABLEBORDER=WIDTH=>

<TR>

<TDalign=center><FONTCOLOR=>

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

<TDalign=center><FONTCOLOR=>

<STRONG>Enteryouremailaddress:</STRONG>

</FONT></TD>

</TR>

<TR>

<TDalign=center><INPUTTYPE=text

NAME=nameSIZE=MAXLENGTH=></TD>

</TD>

<TDalign=center><INPUTTYPE=text

NAME=emailSIZE=MAXLENGTH=></TD>

</TR>

<TR>

<TDalign=center><FONTCOLOR=>

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

<TDalign=center><FONTCOLOR=>

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

</TR>

<TR>

<TDalign=center><INPUTTYPE=text

NAME=browserSIZE=MAXLENGTH=></TD>

<TDalign=center><INPUTTYPE=text

NAME=countrySIZE=MAXLENGTH=>

</TD>

</TR>

</TABLE>

<CENTER>

<FONTCOLOR=><STRONG>

Leaveacommentorsuggestion:</strong></font><BR>

<TEXTAREANAME=comments

ROWS=COLS=wrap=yes>Comments?

Suggestions?</TEXTAREA><P><P>

<FONTCOLOR=><STRONG>

Listyourfavoritesites:</STRONG></FONT><BR>

<TDalign=center><TEXTAREANAME=favorites

ROWS=COLS=wrap=yes>

AnysitesIshouldtakealookat?</TEXTAREA>

<P><P>

<INPUTTYPE=submitVALUE=Submit>

<center><INPUTTYPE=resetVALUE=Reset

onClick=Reset()>

</FORM>

</CENTER>

</BODY>

</HTML>
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19258.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.