๐ป Programming/Javascript
[Javascript / ์๋ฐ์คํฌ๋ฆฝํธ] ๊ฐ์ข #18 - Validation ( ์ ํจ์ฑ ๊ฒ์ฌ )
์ ํจ์ฑ ๊ฒ์ฌ๋ผ๋ ๋ง์ ์ข ์ด๋ ต์ฃ ? ๊ฐ๋จํ ๋งํ๋ฉด ๊ทธ๋ฅ ๊ฒ์ฌํ๋๊ฒ๋๋ค. ๋ณด๋ด๋ ค๋ ๋ฐ์ดํ๊ฐ ์ ๋๋ก ๋ ํฌ๋งท์ผ๋ก ์ ๋ฌ์ด ๋๋์ง ์๋๋ฉด ํ์ ์
๋ ฅ๊ฐ์ด ๋น์นธ์ผ๋ก ๋จ์์์ง๋ ์์์ง ๋ฑ๋ฑ์ ๋ง์ด์ฃ .
์
๋ฐ์คํฌ๋ฆฝํธ์์ ์ ํจ์ฑ ๊ฒ์ฌ๋ โํฌ๊ฒ ๋๊ฐ์ง๋ก ๋๋๋๋ค. ๊ธฐ๋ณธ ๊ฒ์ฌ์ ํฌ๋งท๊ฒ์ฌ. ๊ธฐ๋ณธ๊ฒ์ฌ์์๋ ํ์์
๋ ฅ๊ฐ์ด ๋น์นธ์ผ๋ก ๋จ์์์ง๋
์์์ง์ ๋ํ ๊ฒ์ฌ๋ฅผ ํฉ๋๋ค. ๊ทธ๋ฆฌ๊ณ ํฌ๋งท๊ฒ์ฌ์์๋ ์
๋ ฅํ ๋ฐ์ดํ์ ํฌ๋งท์ด ์๋ฒ์์ ํ์๋ก ํ๋ ํฌ๋งท์ด ๋ง๋์ง๋ฅผ ๊ฒ์ฌ๋ฅผ ํ๊ฒ
๋ฉ๋๋ค.
์์ ๋ฅผ ํ๋ฒ ๋ณผ๊น์?? ๊ฒ์ฌ๋ฅผ ํ ๋๋ ์๋์ฒ๋ผ ์ฝ๋ฉ์ ํ๊ฒ ๋ฉ๋๋ค. ๊ฒ์ฌ๋ฅผ ํ ๋ ํ๋์ ํ ์คํธํ๋ ๋จ์๋ก ํ ์๋ ์์ ๊ฒ์ด๊ณ ๋ชจ๋ ํ๋๋ฅผ ๋์์ผ๋ก ๊ฒ์ฌ๋ฅผ ์งํํ ์๋ ์์ํ ๋ฐ ์ด๊ฑด ๊ฐ๋ฐ์์ ์ฌ๋๊ป ์ด๋ค์์ผ๋ก ๊ตฌํํ๊ฒ ๋ค~๋ผ๊ณ ์ ํ๋ฉด ๋ ๊ฒ ๊ฐ๋ค์.
<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