2016-08-13 259 views
0

我有一個表格都得到驗證,除了電話號碼,我試了又試,沒有任何成功,我可以找出其中的問題打下電話號碼沒有得到驗證

這是我的看法

<div class="form-group"> 
     @Html.LabelFor(m => m.Cell, htmlAttributes: new { @class = "col-md-2 control-label" }) 
     <div class="col-md-4"> 
      @Html.TextBoxFor(m => m.Cell, new { @class = "form-control", id = "Cell", placeholder = "Phone Number" }) 
      @Html.ValidationMessageFor(m => m.Cell, "", new { @class = "text-danger" }) 
</div> 
</div> 

的Javascript

<script> 
    jQuery(document) 
     .ready(function() { 

      jQuery("#Cell").inputmask("099 999 9999"); 

      jQuery("#Cell") 
       .on("blur", 
        function() { 
         var last = $(this).val().substr($(this).val().indexOf("-") + 1); 
         if (last.length == 4) { 
          var move = $(this).val().substr($(this).val().indexOf("-") - 1, 1); 
          var lastfour = move + last; 
          var first = $(this).val().substr(0, 9); 
          $(this).val(first + '-' + lastfour); 
         } 
        }); 

     }); 
</script> 

型號

public class FeedbackViewModel 
{ 
    [MinLength(10)] 
    [StringLength(13, ErrorMessage = "Please enter a valid phone number")] 
    public string Cell { get; set; } 
} 
+0

什麼是您所遇到的當前行爲,什麼是預期的行爲? – Shyju

+0

@Shyju如果電話號碼少於10或者沒有應該說的號碼,則表格不能發佈POST請輸入電話號碼 –

+0

如果您將代碼格式化爲無需滾動,則更容易得到響應。 – zhon

回答

0

我知道了這就是我做了一個模型和視圖

[Required(ErrorMessage = "Your must provide a Phone Number")] 
     [Display(Name = "Cell")] 
     [DataType(DataType.PhoneNumber)] 
     [RegularExpression(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$", ErrorMessage = "Not a valid Phone number")] 
     public string Cell { get; set; } 


@Html.EditorFor(m => m.Cell, new { htmlAttributes = new { @class = "form-control", id="Cell", placeholder = "Phone Number" } })