본문 바로가기

닷컴's_열공/jQueryJavaScript/Ajax

jQuery 문자열체크(숫자,영어,한글)

 

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
 <%
  String root = request.getContextPath();
 %>   
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "
http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script language="javascript" src="<%=root%>/jquery/jquery-1.3.2.min.js"></script>
<script language="javascript">
 
 //마지막 글자 추출
 function lastLan(val){
  var length = val.length;
  var str = val.substr(length-1,length);
  return str;
 }

 /* --------------------------------------------------
 알파벳인지 체크
 ------------------------------------------------*/
 function isAlphabet(ch) {
  var numUnicode = ch.charCodeAt(0); // number of the decimal Unicode
  if ( 65 <= numUnicode && numUnicode <= 90 ) return true;            // 대문자
  if ( 97 <= numUnicode && numUnicode <= 122 ) return true;            // 소문자
  return false;
 }

 /* --------------------------------------------------
 한글인지 체크
 ------------------------------------------------*/
 function isKorean(ch) {
  var numUnicode = ch.charCodeAt(0);
  if ( 44032 <= numUnicode && numUnicode <= 55203 || 12593 <= numUnicode && numUnicode <= 12643 ) return true;            
 return false;
 }

 /* --------------------------------------------------
 숫자인지 체크
 ------------------------------------------------*/
 function isNumber(ch) {
  var numUnicode = ch.charCodeAt(0);                                                                                    
  if ( 48 <= numUnicode && numUnicode <= 57 ) return true;            
 return false;
 }

 function langChk(attribute){
  var rule =  attribute.vcheck;
  var value = attribute.value;
  var str = lastLan(value);
  var chk = false; 
  
  if(rule){
   if(rule.indexOf("en") < 0 && !chk && str){
    chk = isAlphabet(str);
   }
   if(rule.indexOf("int") < 0 && !chk && str){
    chk = isNumber(str);
   }
   if(rule.indexOf("kr") < 0 && !chk && str){
    chk = isKorean(str);
   }
  }
  
  if(chk){
   alert('형식에 맞는 문자 및 숫자만 입력가능합니다.');
   attribute.value = "";
  }
  
   
 }
 
 $(document).ready(function(){  
  $("input[vcheck]").keyup(function(event){
   langChk(this); 
  });
 });
</script>
<title>jQuery ValidateCheck</title>
</head>
<body>
  
  
      
 <table align="center" cellpadding="0" cellspacing="0" border="0">
  <tr>
   <td width="50%">영문 + 숫자</td>
   <td width="50%">
    <input type="text" id="en_int" name="en_int" vcheck="en int">
   </td>
  </tr>
  <tr>
   <td>한글 + 숫자</td>
   <td>
    <input type="text" id="kr_int" name="kr_int" vcheck="kr int">
   </td>
  </tr>
  
  <tr>
   <td>한글</td>
   <td>
    <input type="text" id="kr" name="kr" vcheck="kr">
   </td>
  </tr>
  
  <tr>
   <td>영문</td>
   <td>
    <input type="text" id="en" name="en" vcheck="en">
   </td>
  </tr>
  
  <tr>
   <td>숫자</td>
   <td>
    <input type="text" id="int" name="int" vcheck="int">
   </td>
  </tr>
  
  
  <tr>
   <td>노벨데이션</td>
   <td>
    <input type="text" id="noval" name="noval" >
   </td>
  </tr>
  
 </table>   
    
    
</body>
</html>

[출처] jQuery 문자열체크(숫자,영어,한글)|작성자 시네루