2014-02-14 2 views
0

안녕하세요, 제출 버튼이있는 기본 입력란 하나가 있으며 입력 한 문자 만 자동으로 지 웁니다. 입력 필드에 숫자가 없으면 입력 필드에 텍스트없이 제출하지 않도록하고 싶습니다. 문제는 버튼이 라이브 링크라는 것입니다. 감사.입력란에 텍스트없이 제출 금지하지 않습니다.

바이올린 다음 is_valid_zip() 함수를 작성 http://jsfiddle.net/u3RZV/

$(document).ready(function() { 
     $("#quantity").keypress(function (e) { 
      if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) { 
       $("#errmsg").html("Enter Valid Zip!").show().fadeOut("5000"); 
       return false; 
      } 
     }); 
    }); 

#errmsg { 
    font:12px normal Futura, sans-serif; 
    background:yellow; 
    color:#444444; 
} 
.box { 
    width: 85px; 
    margin-bottom:10px; 
} 
.boxtwo { 
    width: 160px; 
} 
input { 
    width: 100%; 
    background: #FFFFFF; 
    border-radius: 4px; 
    border: 2px solid #acd50b; 
    padding: 10px 15px; 
    text-align: center; 
    -webkit-appearance: none; 
    color: #999999; 
    font-size: 11px; 
} 
input[type="focus"] { 
    outline: none; 
} 
input[type="submit"] { 
    border-radius: 4px; 
    border: 2px solid #2d8b1b; 
    -webkit-appearance: none; 
    background: #acd50b; 
    padding: 6px 10px; 
    color: #444444; 
    width: 100%; 
    font-size: 18px; 
    cursor:pointer; 
} 
<form action=""> 
    <div class="box"> 
     <input type="text" id="quantity" value="Enter Your Zip" /><span id="errmsg"></span> 

    </div> 
    <div class="boxtwo"> <a href="buttons.html"> 
      <input type="submit" id="signup" value="Compare"/> 
      </a> 

    </div> 
</form> 

답변

0
$("form").on("submit", function(e) { 
    if (!is_valid_zip($("#quantity").val())) { 
     e.preventDefault(); 
    } 
}); 

당신에게 달려 있습니다. alert() 또는 이와 유사한 방법을 통해 사용자에게 오류 메시지를 표시하려고 할 수도 있습니다.

+0

'if (e.preventDefault) e.preventDefault(); else e.returnValue = false; ' – user13500

+0

필요 없음 - jQuery의 이벤트 모델은 그 차이점을 추상화합니다. – greenie2600

관련 문제