转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 站长学院 >> Web开发 >> 正文
几个很有用的javascript函数         ★★★★

几个很有用的javascript函数

作者:闵涛 文章来源:闵涛的学习笔记 点击数:604 更新时间:2009/4/23 11:21:16
  函数Reset()按Reset按钮后对各字段的内容复位。



  函数submitForms()按submit按钮后对字段合法性检查后发送电子邮件。



  函数isName()对姓名字段进行合法性检查。



  函数isEmail()对电子邮件地址字段进行合法性检查。



  函数isBrowser()对浏览器字段与自动检测的浏览器版本进行比较。



  函数isCountry()对国家字段进行合法性检查。



  函数isComment()对意见字段进行合法性检查,不允许为空值。



  函数isFavorite()对喜欢的站点字段进行合法性检查,不允许为空值。



  程序中还提供了一些技巧,例如,如何判断浏览器的版本,字符串的操作等等。



  结果是以电子邮件的形式提供给你的,里面有客人输入的各个字段。程序比较长,但不难看懂,下面是源代码:





<HTML>

<HEAD>

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

<SCRIPTLANGUAGE="JavaScript">

<!--Begin

//Someone@abc.com是你自己的电子邮件地址

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\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'

onSubmit="returnsubmitForms()">

<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"

onClick="Reset()">

</FORM>

</CENTER>

</BODY>

</HTML>


没有相关教程
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · Web开发  · 网页制作
    · 平面设计  · 网站运营
    · 网站推广  · 搜索优化
    · 建站心得  · 站长故事
    · 互联动态
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台