μœ νš¨μ„± κ²€μ‚¬λΌλŠ” 말은 μ’€ μ–΄λ ΅μ£ ? κ°„λ‹¨νžˆ λ§ν•˜λ©΄ κ·Έλƒ₯ κ²€μ‚¬ν•˜λŠ”κ²λ‹ˆλ‹€. λ³΄λ‚΄λ €λŠ” 데이타가 μ œλŒ€λ‘œ 된 포맷으둜 전달이 λ˜λŠ”μ§€ μ•„λ‹ˆλ©΄ ν•„μˆ˜ μž…λ ₯값이 빈칸으둜 λ‚¨μ•„μžˆμ§€λŠ” μ•Šμ€μ§€ 등등을 말이죠.
자 λ°”μŠ€ν¬λ¦½νŠΈμ—μ„œ μœ νš¨μ„± κ²€μ‚¬λŠ” β€‹ν¬κ²Œ λ‘κ°€μ§€λ‘œ λ‚˜λˆ•λ‹ˆλ‹€. κΈ°λ³Έ 검사와 포맷검사. κΈ°λ³Έκ²€μ‚¬μ—μ„œλŠ” ν•„μˆ˜μž…λ ₯값이 빈칸으둜 λ‚¨μ•„μžˆμ§€λŠ” μ•Šμ€μ§€μ— λŒ€ν•œ 검사λ₯Ό ν•©λ‹ˆλ‹€. 그리고 ν¬λ§·κ²€μ‚¬μ—μ„œλŠ” μž…λ ₯ν•œ λ°μ΄νƒ€μ˜ 포맷이 μ„œλ²„μ—μ„œ ν•„μš”λ‘œ ν•˜λŠ” 포맷이 λ§žλŠ”μ§€λ₯Ό 검사λ₯Ό ν•˜κ²Œ λ©λ‹ˆλ‹€.

 

예제λ₯Ό ν•œλ²ˆ λ³ΌκΉŒμš”?? 검사λ₯Ό ν• λ•ŒλŠ” μ•„λž˜μ²˜λŸΌ 코딩을 ν•˜κ²Œ λ©λ‹ˆλ‹€. 검사λ₯Ό ν•  λ•Œ ν•˜λ‚˜μ˜ ν…μŠ€νŠΈν•„λ“œ λ‹¨μœ„λ‘œ ν•  μˆ˜λ„ μžˆμ„ 것이고 λͺ¨λ“  ν•„λ“œλ₯Ό λŒ€μƒμœΌλ‘œ 검사λ₯Ό 진행할 μˆ˜λ„ μžˆμ„ν…λ° 이건 개발자의 μž¬λŸ‰κ» μ–΄λ–€μ‹μœΌλ‘œ κ΅¬ν˜„ν•˜κ² λ‹€~라고 μ •ν•˜λ©΄ 될 것 κ°™λ„€μš”. 

<html>
<head>
<title>Form Validation</title>
<script type="text/javascript">
<!--
// Form validation code will come here.
//-->
</script>
</head>
<body>
 <form action="/cgi-bin/test.cgi" name="myForm"  
          onsubmit="return(validate());">
 <table cellspacing="2" cellpadding="2" border="1">
 <tr>
   <td align="right">Name</td>
   <td><input type="text" name="Name" /></td>
 </tr>
 <tr>
   <td align="right">EMail</td>
   <td><input type="text" name="EMail" /></td>
 </tr>
 <tr>
   <td align="right">Zip Code</td>
   <td><input type="text" name="Zip" /></td>
 </tr>
 <tr>
 <td align="right">Country</td>
 <td>
 <select name="Country">
   <option value="-1" selected>[choose yours]</option>
   <option value="1">USA</option>
   <option value="2">UK</option>
   <option value="3">INDIA</option>
 </select>
 </td>
 </tr>
 <tr>
   <td align="right"></td>
   <td><input type="submit" value="Submit" /></td>
 </tr>
 </table>
 </form>
 </body>
 </html>


기본검사:

μœ„μ—μ„œ λ³Έ κΈ°λ³Έ ν‹€μ—μ„œ validationν•˜λŠ” κΈ°λŠ₯을 κ΅¬ν˜„ν•΄λ³΄μ•˜μŠ΅λ‹ˆλ‹€.:

<script type="text/javascript">
<!--
// Form validation code will come here.
function validate()
{
 
   if( document.myForm.Name.value == "" )
   {
     alert( "Please provide your name!" );
     document.myForm.Name.focus() ;
     return false;
   }
   if( document.myForm.EMail.value == "" )
   {
     alert( "Please provide your Email!" );
     document.myForm.EMail.focus() ;
     return false;
   }
   if( document.myForm.Zip.value == "" ||
           isNaN( document.myForm.Zip.value ) ||
           document.myForm.Zip.value.length != 5 )
   {
     alert( "Please provide a zip in the format #####." );
     document.myForm.Zip.focus() ;
     return false;
   }
   if( document.myForm.Country.value == "-1" )
   {
     alert( "Please provide your country!" );
     return false;
   }
   return( true );
}
//-->
</script>

μ†ŒμŠ€λ₯Ό μ‹€ν–‰ν•΄λ³΄μ„Έμš” ^__^

 

포맷검사:

 μ΄λ²ˆμ—λŠ” 포맷검사λ₯Ό 해보도둝 ν• ν…λ°μš” 이 ν¬λ§·κ²€μ‚¬λŠ” μ–΄λ–€ μž…λ ₯창에 듀어와야 ν•˜λŠ” μž…λ ₯κ°’μ˜ 포맷을 μ •μ˜ν•˜λŠ” 정책이 μžˆμ–΄μ•Ό ν•©λ‹ˆλ‹€. 예λ₯Όλ“€μ–΄ 이메일을 μž…λ ₯ν•΄μ•Όν•˜λŠ” μž…λ ₯λž€μ—λŠ” asdf@bbc.comκ³Ό 같은 ν˜•νƒœμ˜ 이메일 포맷을 κ°–κ³ μžˆλŠ” λ¬Έμžμ—΄μ΄ λ“€μ–΄κ°€μ•Ό ν•œλ‹€λŠ” κ²ƒμž…λ‹ˆλ‹€. μ•„λž˜ μ˜ˆμ œλŠ” 이메일 ν•„λ“œμ— μž…λ ₯된 값이 이메일 ν˜•μ‹μ— λ§žλŠ”μ§€λ₯Ό κ²€μ‚¬ν•΄μ£ΌλŠ” κΈ°λŠ₯μ΄λ„€μš”.

<script type="text/javascript">
<!--
function validateEmail()
{
 
   var emailID = document.myForm.EMail.value;
   atpos = emailID.indexOf("@");
   dotpos = emailID.lastIndexOf(".");
   if (atpos < 1 || ( dotpos - atpos < 2 )) 
   {
       alert("Please enter correct email ID")
       document.myForm.EMail.focus() ;
       return false;
   }
   return( true );
}
//-->
</script>

직접 ν…ŒμŠ€νŠΈ 해보싀거죠?? ^___^

 

 

Reference : http://www.tutorialspoint.com/javascript/javascript_form_validations.htm