7-4 ²������������

¦bºô»Úºô¸ô¤W¡Aªí³æ¬O©M¨Ï¥ÎªÌ¤¬°Êªº³Ì±`¥Î¤è¦¡¡C¦ý¨Ï¥ÎªÌ¶ñ¤Jªº¸ê®Æ¥i¯à¤£²Å¦X­n¨D¡]¨Ò¦p±N©m¦WÄæ¦ì¶ñ¦¨¹q¤l¶l¥ó¡B¹q¤l¶l¥ó¤£¥þ¡B¨­¥÷ÃÒ¦r¸¹©Î«H¥Î¥d¸¹½X¦³»~µ¥¡^¡A³o¨Ç³£¥i¥H¦b¦øªA¾¹ºÝªºµ{¦¡½X¶i¦æÀË´ú¡A¨Ã±N¿ù»~°T®§¦^¶Ç¨Ï¥ÎªÌ¡C¦ý³oºØ¤è¦¡¤£¦ý¼W¥[¤F¦øªA¾¹ªº­t¾á¡A¤]¥[­«¤Fºô¸ô¸ê®Æªº¶Ç¿é¶q¡C¤@­Ó²³æªº¸Ñ¨M¤è®×´N¬O¦b«È¤áºÝ¥H JavaScript ¨Ó¹ïªí³æªº¿é¤J¶i¦æÅçÃÒªº¤u§@¡A¥H½T«Oªí³æ¼Æ¾Ú¦b°e¦Ü¦øªA¾¹«e³£¬O¥¿½TµL»~¡A³o­ÓÀˮ֪í³æ¸ê®Æ¬O§_¥¿½Tªº¹Lµ{¡A´NºÙ¬°¡uªí³æ¸ê®ÆÅçÃÒ¡v¡]Form Data Validation¡^¡A²ºÙ¡uªí³æÅçÃÒ¡v¡]Form Validation¡^¡C

ªí³æÅçÃÒ¥i¥Î JavaScript ªº¦UºØ¦r¦ê»P¼Æ­Èªº¨ç¦¡¨Ó¹F¦¨¡A¨Ã°t¦X·Æ¹«¨Æ¥ó¡A¦b¾A·íªº®É¾÷¨Ó´£¿ô¨Ï¥ÎªÌ¥i¯àµo¥Íªº¿ù»~¡C¥H JavaScript ¦b«È¤áºÝ¶i¦æªí³æÅçÃÒªº¥D­n¦n³B¥iºK­n¦p¤U¡G¡G

¦ý¬O¡A­Y¶È¶È¨Ï¥Î«È¤áºÝªºµ{¦¡½X¨Ó¶i¦æªí³æÅçÃÒ¡A¥u¯à±Æ°£µL¤ßªº¿ù»~¡A¹ï©ó¦³¤ß­n±q¨Æ¯}Ãa¤u§@ªº´c·N¨Ï¥ÎªÌ¡Aªí³æÅçÃÒ¬OµLªk°µ¨ìºw¤ô¤£º|ªº¡C´«¥y¸Ü»¡¡A¹ï©ó­«­nªº¸ê®Æ¡A°£¤F¦b¥Î¤áºÝ¶i¦æªíºÝÅçÃÒ¥~¡A³Ì¦n¤]¦b¦øªA¾¹¶i¦æ¬ÛÃö¸ê®ÆªºÀˬd¡C

¦¹¥~¡ANetscape ¤Î IE ¦b²Ä¥|ª©«á³£¤ä´© JavaScript ªº¡u³q¥Îªí¥Üªk¡v¡]Regular Expressions¡^¡A³o¬O¤@®M¥\¯à«D±`±j¤jªº¦r¦ê¤ñ¹ï¤èªk¡A³o¨Ï±oªí³æÅçÃÒªºµ{¦¡½X¯q§Î²¼ä¡C¦³Ãö³q¥Îªí¥Üªk¡A·|¦b¥»®Ñ«áÄò³¹¸`¦³¸Ô²Óªº¤¶²Ð¡C¦ý¥»¸`ªº­«ÂI«h¬O¥H²¼äªº½d¨Ò¨Ó»¡©úªí³æÅçÃÒªº·§©À¡A¤£·|¨Ï¥Î³q¥Îªí¥Üªk¡A¥H«KÅýŪªÌ¯à°÷¶i¤J±¡ªp¡C

­º¥ý§Ú­Ì¨Ó¬Ý¤@­Ó²³æªº½d¨Ò¡A¦¹½d¨ÒÀˬd¨Ï¥ÎªÌ¿é¤Jªº¹q¤l¶l¥ó¡AŪªÌ¥i¥H¦Û¦æ¿é¤J¹q¤l¶l¥ó±b¸¹¨Ó¸Õ¸Õ¬Ý¡G

Example¡]formValidation01.htm¡^¡G

¦b¤W­z½d¨Ò¤¤¡A§Ú­ÌÀËÅç¹q¤l¶l¥óªº³W«h«Ü²³æ¡A¦CÁ|¦p¤U¡G

¦¹½d¨Òªº­ì©lÀɦp¤U¡G

­ì©lÀÉ¡]formValidation01.htm¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>²©öªí³æ¸ê®ÆÅçÃÒ¡G¹q¤l¶l¥ó</h2>
<hr>

<script>
function validateForm(form){
	if (!checkEmail(form.email.value)){
		alert("Email ¸ê®Æ¦³»~¡Aªí³æ±N¤£°e¥X¡I");
		return(false);	
	}
	alert("¸ê®Æ¥¿½TµL»~¡A¥ß¨è°e¥Xªí³æ¡I");
	form.submit();
	return(true);
}

function checkEmail(email){
	index = email.indexOf ('@', 0);		// ´M§ä @ ªº¦ì¸m¡A0 ¥Nªí¶}©l´M§äªº°_©l¦ì¸m
	if (email.length==0) {
		alert("½Ð¿é¤J¹q¤l¶l¥ó¦a§}¡I");
		return (false);
	} else if (index==-1) {
		alert("¿ù»~¡G¥²¶·¥]§t¡u@¡v¡C");
		return (false);
	} else if (index==0) {
		alert("¿ù»~¡G¡u@¡v¤§«e¤£¥i¬°ªÅ¦r¦ê¡C");
		return (false);
	} else if (index==email.length-1) {
		alert("¿ù»~¡G¡u@¡v¤§«á¤£¥i¬°ªÅ¦r¦ê¡C");
		return (false);
	} else
		return (true);
}
</script>

½Ð¿é¤J±z­Ó¤Hªº¹q¤l¶l¥ó¡G<p>
<form>
¹q¤l¶l¥ó¡G
<input type="text" name=email size=20 value="xxxx"><br>
<p><input type=button VALUE="¬d®Ö" onClick="validateForm(this.form)">
</form>

<hr>
</body>
</html>

ÄY®æ¨Ó»¡¡A³o¨ÇÅçÃÒ³W«h¬O¹L©ó²³æ¡A·í§Ú­Ì¾Ç¨ì³q¥Îªí¥Üªk®É¡A´N¥i¥H¹ï¹q¤l¶l¥ó¶i¦æ¸û½ÆÂø§¹¾ãªºÅçÃÒ¡C

¦b¤U­±ªº½d¨Ò¡A§Ú­Ì¹ï«H¥Î¥d¸¹½X¶i¦æÅçÃÒ¡G

Example¡]formValidation02.htm¡^¡G

¦b¤W­z½d¨Ò¤¤¡A§Ú­Ì°²³]«H¥Î¥d¥d¸¹ªº®æ¦¡¬O¡uxxxx-xxxx-xxxx-xxxx¡v¡A¨ä¤¤ªº¨C¤@­Ó x ³£¬O¥Nªí¤@­Ó¼Æ¦r¡A§Ú­ÌªºÅçÃÒ³W«h¬O¡G

¤W­z½d¨Òªº­ì©lÀɦp¤U¡G

­ì©lÀÉ¡]formValidation02.htm¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>ªí³æ¸ê®ÆÅçÃÒ¡G«H¥Î¥d¸¹½X</h2>
<hr>

<script>
function validateForm(form){
	if (checkCreditCardNumber(form.cardNumber)){
		alert("¸ê®Æ¥¿½TµL»~¡A¥ß¨è°e¥Xªí³æ¡I");
		form.submit();
		return(true);
	}
	alert("¸ê®Æ¦³»~¡Aªí³æ±N¤£°e¥X¡I");
	form.cardNumber.focus();
	return(false);
}

function checkCreditCardNumber(control){
	var number=control.value;
	var character;
	var digit;
	if (number.length!=19){
		alert("<"+number+">¡G¥d¸¹ªø«×¦³»~¡A½Ð¬d®Ö¡I");
		return false;
	}
	for (i=0; i<19; i++){
		character=number.charAt(i);
		if ((i==4)||(i==9)||(i==14)){	// Àˬd¬O§_¬O "-"
			if (character!="-"){
				alert("<"+number+">¡G¥d¸¹¿é¤J¦³»~¡]½Ð¥H¡u-¡v¤À¶}¥|½X¡^¡A½Ð¬d®Ö¡I");
				return false;
			}
		} else {	// Àˬd¬O§_¬O¼Æ¦r
			if (isNaN(parseInt(character))){
				alert("<"+number+">¡G¥d¸¹¿é¤J¦³»~¡A½Ð¬d®Ö¡I");
				return false;
			}
		}
	}
	return true;
}
</script>

½Ð¿é¤J±z­Ó¤Hªº«H¥Î¥d¸ê®Æ: <p>
<form>
¥d¸¹:
<input type="text" name=cardNumber size=20 value="xxxx-xxxx-xxxx-xxxx"><br>
<p><input type=button VALUE="¬d®Ö" onClick="validateForm(this.form)">
</form>

<hr>
</body>
</html>

¨Æ¹ê¤W¤W­zÅçÃÒ³W«h¹L©ó²ÊÁW¡A¯u¥¿ªº«H¥Î¥d¸¹½XÁÙ­n²Å¦X¤@¨Ç¤º­qªº½s½X³W«h¡A¸Ô¨£¥»®Ñ«áÄò¦³Ãö©ó¡u³q¥Îªí¥Üªk¡v¤§³¹¸`ªº»¡©ú¡C

¦b¤U¦Cªº½d¨Ò¤¤¡A§Ú­Ì¹ï¦UÃþªí³æ±±¨î¶µ¶i¦æ¸ê®ÆÅçÃÒ¡A¬ÛÃö³W«h³£¥i¥H¥Ñµ{¦¡½X¬Ý¥X¡A½Ð¦U¦ìŪªÌ¿Ë¦Û¸Õ¬Ý¬Ý³o­Ó½d¨Ò¡G

Example¡]formValidation03.htm¡^¡G

¤W­z½d¨Òªº­ì©lÀɦp¤U¡G

­ì©lÀÉ¡]formValidation03.htm¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>ªí³æ¸ê®ÆÅçÃÒ¡GÅçÃÒ¦UÃþ±±¨î¶µªº¸ê®Æ</h2>
<hr>

<script>
function checkName(control) {
	if (control.value == "") {
		validatePrompt(control, "½Ð¿é¤J¤j¦W¡I");
		return (false);
	}
	return (true);
}

function checkPassword(control) {
	if (control.value.length != 5) {
		validatePrompt(control, "½Ð¿é¤Jªø«×¬° 5 ªº±K½X¡I");
		return (false);
	}
	return (true);
}

function checkGender(control) {
	for (i=0; i<control.length; i++)
		if (control[i].checked)
			return(true);
	alert ("½Ð¿é¤J©Ê§O¡I");
	return (false);
}

function checkNationality(control) {
	for (i=1; i<control.length; i++)
		if (control[i].selected)
			return(true);
	alert ("½Ð¿é¤J°êÄy¡I");
	return (false);
}

function checkHobbies(control) {
	var count=0;
	for (i=0; i<control.length; i++)
		if (control[i].selected)
			count++;
	if (count>=2)
		return(true);
	alert ("½Ð¿é¤J¦Ü¤Ö¨â¼Ë¿³½ì¡I");
	return (false);
}

function checkComment(control) {
	if (control.value == "") {
		validatePrompt(control, "½Ð¿é¤J±zªºÄ_¶Q·N¨£¡I");
		return (false);
	}
	return (true);
}

function validateForm(form) {
	if (!checkName(form.realname)) return;
	if (!checkPassword(form.password)) return;
	if (!checkGender(form.gender)) return;
	if (!checkNationality(form.nationality)) return;
	if (!checkHobbies(form.hobbies)) return;
	if (!checkComment(form.comments)) return;
	alert ("§A²×©óÅ¥§Úªº¸Ü¤F¡I\n¥þ³¡¸ê®Æ³q¹LÅçÃÒ¡I\nªí³æ§Y±N°e¥X¡I¡I¡I");
	document.testform.submit();	// Submit form
}

function validatePrompt(control, promptStr) {
	alert(promptStr);
	control.focus();
	return;
}
</script>

<form name=testform>
<P>¶Q©Ê¤j¦W¡G<input type="text" name="realname" value="xxx">
<P>­Ó¤H±K½X¡G<input type="password" name="password">
<P>±zªº©Ê§O¡G<input type="radio" name="gender" value="male"> male
	<input type="radio" name="gender" value="female"> female
<P>±zªº°êÄy¡G<select name="nationality">
	<option>----<option>¥xÆW<option>¤é¥»<option>Áú°ê
	</select>
<P>±zªº¿³½ì¡G<select multiple name="hobbies">
	<option>¥´²y<option>¤U´Ñ<option selected>Å¥­µ¼Ö<option>¾\Ū<option>¤Wºô
	</select>
<p>Ä_¶Q·N¨£¡G<textarea name=comments cols=40 rows=3></textarea>
<P><input type="button" value="°e¥X" onClick="validateForm(this.form)"><input type="reset">
</form>

<hr>
</body>
</html>


JavaScript µ{¦¡³]­p»PÀ³¥Î¡G¥Î©óºô­¶¥Î¤áºÝ