2016-08-13 263 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" } })