javascript

位置:IT落伍者 >> javascript >> 浏览文章

几个很有用的javascript函数


发布日期:2022年01月12日
 
几个很有用的javascript函数
函数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>               

上一篇:一实用的Javascript类库(表格排序)

下一篇:JavaScript中的数组、集合及效率