首页 > 教學 > html > javascript判斷欄位是否有輸入
2011
12-28

javascript判斷欄位是否有輸入

<SCRIPT  LANGUAGE="JavaScript">

		function checkEmail(checkString) {
			var newstr = "";
			var at = false;
			var dot = false;

		// DO SOME PRELIMINARY CHECKS ON THE DATA

		// IF EMAIL ADDRESS HAS A '@' CHARACTER
		if (checkString.indexOf("@") != -1) {
			at = true;

		// IF EMAIL ADDRESS HAS A '.' CHARACTER
		} else if (checkString.indexOf(".") != -1) {
			dot = true;
		}

		// PARSE REMAINDER OF STRING
		for (var i = 0; i < checkString.length; i++) {
			ch = checkString.substring(i, i + 1)
			if ((ch >= "A" && ch <= "Z") || (ch >= "a" && ch <= "z")
				|| (ch == "@") || (ch == ".") || (ch == "_")
				|| (ch == "-") || (ch >= "0" && ch <= "9")) {
				newstr += ch;
				if (ch == "@") {
					at=true;
				}
				if (ch == ".") {
					dot=true;
				}
			}
		}

		if ((at == true) && (dot == true)) {
			return true;
		}
		else {
			// DISPLAY ERROR MESSAGE
			alert("電子郵件不正確");
			return false;
			}
		}

		function checkPassword(Password1,Password2)
		{
			if(Password1==Password2)
			   return true;
			else
			{
				// DISPLAY ERROR MESSAGE
				alert("密碼跟密碼確認不相同");
				return false;
			}
					
			
		}
		

		function check_value(check_name)
		{
		   n=0;
		   for(i=0;i<check_name.length;++i)
		     if(check_name[i].checked)
		        n=check_name[i].value;
	   	  return n;
		}


		function checkRequiredFields(input) {
		var requiredFields = new Array("PersonalName",
						"Email","Password","ConfirmPassword","Sex","yy","mm","dd","Mobile","ZipCode","Address","kind2","GradClass"); 


		 var fieldNames = new Array("申請入學學生姓名",
			"有效確認用電子郵件信箱(帳號)","密碼","密碼確認","性別","出生年","出生月","出生日","可資有效連繫之行動電話或住家電話","郵遞區號","地址","畢業國中","畢業班級"); 

		var fieldCheck   = true;
		var fieldsNeeded = "請填寫以下資料:\n\n\t";

		for(var fieldNum=0; fieldNum < requiredFields.length; fieldNum++) {
			if ((input.elements[requiredFields[fieldNum]].value == "") ||
				(input.elements[requiredFields[fieldNum]].value == " ")  ||
				(input.elements[requiredFields[fieldNum]].value == "請選擇")) {

				fieldsNeeded += fieldNames[fieldNum] + "\n\t";
				fieldCheck = false;
			}
		}



		Branch1=input.elements['Branch1'].value;
		Branch2=input.elements['Branch2'].value;
		Branch3=input.elements['Branch3'].value;
		if(input.elements['kind2'].value=='-1')
		{
			fieldsNeeded += '畢業國中' + "\n\t";
			fieldCheck = false;				
		}
		if(input.elements['Branch1'].value=='請選擇科別')
		{
			fieldsNeeded += '我最有興趣的科別' + "\n\t";
			fieldCheck = false;				
		}
		if(input.elements['Branch2'].value=='請選擇科別')
		{
			fieldsNeeded += '我其次有興趣的科別' + "\n\t";
			fieldCheck = false;				
		}
		if(input.elements['Branch3'].value=='請選擇科別')
		{
			fieldsNeeded += '我其他有興趣的科別' + "\n\t";
			fieldCheck = false;				
		}
		
		if((Branch1==Branch2 || Branch1==Branch3) && Branch1!='請選擇科別')
		{
			fieldsNeeded += '我最有興趣的科別與其它科別重覆' + "\n\t";
			fieldCheck = false;				
		}

		if((Branch2==Branch1 || Branch2==Branch3) && Branch2!='請選擇科別')
		{
			fieldsNeeded += '我其次有興趣的科別與其它科別重覆' + "\n\t";
			fieldCheck = false;				
		}
		if((Branch3==Branch2 || Branch3==Branch1) && Branch3!='請選擇科別')
		{
			fieldsNeeded += '我其他有興趣的科別與其它科別重覆' + "\n\t";
			fieldCheck = false;				
		}

				
		if(input.elements['confirm01'].checked==false)
		{
				fieldsNeeded += '確認事項1無勾選'+ "\n\t";
				fieldCheck = false;
		}
		if(input.elements['confirm02'].checked==false)
		{
				fieldsNeeded += '確認事項2無勾選' + "\n\t";
				fieldCheck = false;
		}
		if(input.elements['confirm03'].checked==false)
		{
				fieldsNeeded += '確認事項3無勾選' + "\n\t";
				fieldCheck = false;
		}
		
		

		if (fieldCheck == true)
			{
			return true;
			}
			// SOME REQUIRED FIELDS ARE MISSING VALUES
			else
			{
			alert(fieldsNeeded);
			return false;
			}
		}

		function verify(obj) {
		if (checkRequiredFields(obj) && checkEmail(obj.Email.value) && checkPassword(obj.Password.value,obj.ConfirmPassword.value)) {
			// submit to server
			obj.submit();
			}
		}
		// end hiding from old browsers -->
		

		
		</SCRIPT>
同步發表於:javascript判斷欄位是否有輸入
最后编辑:
作者:明和 蔡
这个作者貌似有点懒,什么都没有留下。

One Response to javascript判斷欄位是否有輸入

  1. Ok! You can add it to your bookmarks.

留下一个回复

你的email不会被公开。