2012-10-24 40 views
0

我正在使用Jquery mobile和MVC4在VS2010中開發移動應用程序。您能否就如何在提交下面的代碼段中驗證文本框提供建議?我只需要檢查它是否包含一個值。理想情況下,我想在這個div下面放置一條錯誤消息。JQuery移動驗證

<div data-theme="a" class="ui-grid-a"> 
    <div class="ui-block-a">@Html.TextBox("id", "", new { @class = "mystyleclass", onclick = "this.value=''" })</div> 
    <div class="ui-block-b"> 
      <button type="submit" data-theme="b">Search</button> 
    </div> 
</div> 

回答

0

如果您使用的是MVC,此文本框是否連接到模型元素?在這種情況下,在模型中添加上面的[Required]標籤,使其成爲@ html.texboxfor,然後在html元素下面具有validationMessageFor。如果你只是檢查是不是空的,然後做

<script> 
$(":button").click(function(){ 
    if($("#id").val()!=""){ 
     //whatever you're trying to submit  
    } 
    else{ 
     //show your error message 
    } 
}); 
</script> 

在jQuery中。你也應該在按鈕上使用id,這樣你就不必使用:按鈕選擇器。

+0

嗨,文本框沒有連接到模型。如果需要,我可以這樣做,但我是從控制器操作的窗體中獲取文本框中的值。 – user1698316