2009-12-18 2 views
0

양식의 특정 필드가 숫자가 아닌 텍스트인지 확인하려고합니다. 어디서부터 시작하겠습니까?jquery serialize하기 전에 유효성을 검사하십시오.

필드가 숫자이면 직렬화하고, 그렇지 않으면 빈 문자열을 보냅니다. 이유는 PHP가 각 변수의 데이터를 숫자가 아닌 문자열로 간주하기 때문입니다.

이 내 아약스 요청

$.ajax({ 
     type: "POST", 
     url: "lib/calc.php", 
     data: $("#calcQuery").serialize(), 
     dataType: "html", 
     success: function(response) 
     { 
     $("#calcBox").html(response); 
     $("#calcBox").show(); 
     clearForm("#calcQuery"); 

     }, 

인이 내 양식

당신은 Validate plugin를 사용할 필요가
<form id="calcQuery" class="ui-widget-shadow ui-corner-all" style="margin-top: 1em;"> 
    <fieldset class="ui-corner-all" > 
    <table border="0" width="85%"> 
    <tr> 
    <th><nobr><label for="curr_alloc">Current Space Allocation:</label></nobr></th> 
    <td align="right"><input type="text" size="5" name="curr_alloc" id="curr_alloc" /></td> 
    </tr> 
    <tr> 
    <td> 
    <table border="0" style="margin-left: .5em;"> 
    <tr> 
    <td> 
    <input type="radio" name="curr_unit" value="KB" /><label>KB</label> 
    </td> 
    </tr> 
    <tr> 
    <td> 
    <input type="radio" name="curr_unit" value="MB" /><label>MB</label> 
    </td> 
    </tr> 
    <tr> 
    <td> 
    <input type="radio" name="curr_unit" value="GB" checked /><label>GB</label> 
    </td> 
    </tr> 
    <tr> 
    <td> 
    <input type="radio" name="curr_unit" value="TB" /><label>TB</label> 
    </td> 
    </tr> 
    </table> 
    </td> 
    </tr> 

    <tr> 
    <th><nobr><label for="curr_percent">Current Usage Percentage:</label></nobr></th> 

    <td align="right"><input type="text" size="5" name="curr_percent" id="curr_percent" /></td> 
    </tr> 
    <tr> 
    <th><nobr><label for="desired_percent">Desired Usage Percentage:</label></nobr></th> 

    <td align="right"><input type="text" size="5" name="desired_percent" id="desired_percent" /></td> 

    </tr> 
    </table> 
    </fieldset> 

</form> 

답변

1

PHP 함수 is_numeric을 사용해 보셨습니까? 나는 이것이 당신의 probelm를 해결할 생각

2

입니다. 양식 제출에 대해 다양한 규칙을 지정하는 것은 매우 쉽습니다.

1

Vincent Ramdhanie가 말한 것처럼 validate 플러그인이 좋은 옵션입니다.

그러나, 나는 명확히하고 싶은 몇 가지가 있습니다 :

내가 PHP 각각의 문자열로 변수 및 숫자하지의 데이터를 볼 수 있기 때문입니다하고 싶은 이유.

각 필드 값의 유형은 페이지의 javascript/DOM에 관한 한 문자열입니다. 따라서 사용자가 숫자를 입력하더라도 실제로는 문자열입니다. 따라서 어느 경우 든 문자열을 숫자로 파싱해야합니다 (또는 라이브러리가 대신 해줍니다).

두 번째로 기억해야 할 중요한 점은 클라이언트 측 유효성 검사를 쉽게 무시할 수 있기 때문입니다. 서버 측뿐만 아니라 클라이언트 측에서도 실제로 데이터의 유효성을 검사해야합니다. 귀하의 코멘트에 대한 응답으로

업데이트

, 나는 PHP를 잘 알고 아니지만, 빠른 인터넷 검색이 작동한다는 것을 나에게 보여줍니다

$foo = "3.123"; 
$bar = (float) $foo; 

내가 언급 한 바와 같이, 난 PHP 프로그래머가 아니므로 더 나은 방법이있을 수 있습니다.하지만 그렇게해야합니다.

+0

끈? –

+0

@ SimplySeth 당신은 캐스팅/분석해야합니다. 나는 응답을 업데이트 할 것이다. –

관련 문제