Jsの一般的な検証


最近はjs用が多いので、簡単に整理します。
 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>FormVerify</title>
</head>
<script type="text/javascript">
   <!--js trim  ,       -->
	function trim(str){ //         
       return str.replace(/(^\s*)|(\s*$)/g, "");
   }
	function ltrim(str){ //       
       return str.replace(/(^\s*)/g,"");
   }
  	function rtrim(str){ //       
       return str.replace(/(\s*$)/g,"");
   }
	
	<!--         , onSubmit   return-->
	function fsub(){
	
	<!--     ,    ,    ,            -->
		var name=document.getElementById("username");
		//        
	     if(trim(name.value)==""){
		 	alert("      ");
			name.focus();
			return false;
		 }
	
	<!--          6 ,         -->	 
		 var password=document.getElementById("password");
		 var passworda=document.getElementById("passworda");
		 if(password.value.length < 6){
		 	alert("      6 ");
			return false;
		 }
		 if(password.value != passworda.value){
		 	alert("       ");
			return false;
		 }
		 
	<!--     (radio)         ElementsByName -->
	     var sex=document.getElementsByName("radio");
		 var count=0;
		 for(var i=0;i<sex.length;i++){
		 	if(sex[i].checked){
				count++;
			}
		 }
		 if(count==0){
		 	alert("       ");
			return false;
		 }
		
	<!--     (select)   -->
		var category=document.getElementById("category");
		//>1          ,
		if(category.selectedIndex < 1){
			alert("     ");
			return false;
		}
	<!--    (checkbox   )-->
		var hobby=document.getElementsByName("hobby");
		var flag=false;
		for(var i=0;i<hobby.length;i++){
			 if(hobby[i].checked){
			 	flag=true;
				break;
			 }
		}
		if(!flag){
			alert("        ");
		}
		return flag;
	
		 
	}

</script>
<body>
<form id="form1" name="form1" method="post" action="" onsubmit="return fsub()">
  <table width="80%" border="1">
    <tr>
      <td align="right">   :</td>
      <td><input type="text" name="username" id="username" /></td>
    </tr>
    <tr>
      <td align="right">  :</td>
      <td><input type="password" name="password" id="password" /></td>
    </tr>
    <tr>
      <td align="right">     :</td>
      <td><input type="password" name="passworda" id="passworda" /></td>
    </tr>
    <tr>
      <td align="right">  :</td>
      <td> 
      <input type="radio" name="radio" id="man" value="man" />
       
      <input type="radio" name="radio" id="women" value="women" /></td>
    </tr>
    <tr>
      <td align="right">  :</td>
      <td>
      <select name="category" id="category">
      	<option value="">   </option>
        <option value="work">  </option>
        <option value="firend">  </option>
        <option value="family">  </option>
      </select>
      </td>
    </tr>
    <tr>
      <td align="right">  :</td>
      <td>
        <input type="checkbox" name="hobby" id="hobby" value="movic"/>
          
        <input type="checkbox" name="hobby" id="hobby" value="travel"/>
          
        <input type="checkbox" name="hobby" id="hobby" value="poker"/>
          
      </td>
    </tr>
    <tr>
      <td align="right">
        <input type="submit" name="btnsubmit" id="btnsubmit" value="  "  />
      </td>
      <td><input type="reset" name="btnresult" id="btnresult" value="  " /></td>
    </tr>
  </table>
</form>

</body>
</html>