我是ASP .NET MVC 4的新手。
我正在对Pan Card页面进行验证。
我想做,第一个5个文本框只能插入一个字符,然后下一个4个文本框只能插入一个数字,最后一个文本框只能接受一个字符。
@using (Html.BeginForm())
{
<input type="text" pattern="[A-Z]{1}" style="background-color:cyan; height:20px;width:20px;" name="txt1st" id="txt1st" required />
<input type="text" pattern="[A-Z]{1}" style="background-color:cyan; height:20px;width:20px;" name="txt2nd" id="txt2nd" required />
<input type="text" pattern="[A-Z]" style="background-color:cyan; height:20px;width:20px;" name="txt3rd" id="txt3rd" required />
<input type="text" pattern="[A-Z]" style="background-color:wheat; height:20px;width:20px;" readonly value="P" name="txt4th" id="txt4th" />
<input type="text" pattern="[A-Z]" style="background-color:cyan; height:20px;width:20px;" name="txt5th" id="txt5th" required />
<input type="text" pattern="[0-9]" style="background-color:blue; color:wheat;height:20px;width:20px;" name="int6th" id="int6th" required />
<input type="text" pattern="[0-9]" style="background-color: blue; color: wheat; height: 20px; width: 20px;" name="int7th" id="int7th" required />
<input type="text" pattern="[0-9]" style="background-color: blue; color: wheat; height: 20px; width: 20px;" name="int8th" id="int8th" required />
<input type="text" pattern="[0-9]" style="background-color: blue; color: wheat; height: 20px; width: 20px;" name="int9th" id="int9th" required />
<input type="text" pattern="[A-Z]" style="background-color:cyan; height:20px;width:20px;" name="txt10th" id="txt10th" required /><br />
<input type="submit" class="btnPrimary" value="Update" />
}
我不知道如何执行此操作以及如何对每个文本框进行验证,我不应该在文本框中输入第二个值。
谁能告诉我如何解决这个问题?
看来您根本没有使用MVC提供的功能。1.根据您的要求创建模型,例如,
public class PANCardDetails
{
[RegularExpression("Your regyular expr. to validate")]
public string Number1 { get; set; }
// Do define the other properties like the above
}
使用以下模型修改视图中的表单,
@model PANCardDetails
@using (Html.BeginForm())
{
Html.EnableClientValidation(true);
Html.EnableUnobtrusiveJavaScript(true);
@Html.TextBoxFor(pancardDetails=>pancardDetails.Number1)
@Html.ValidationMessageFor(pancardDetails=>pancardDetails.Number1)
//Similarly create the controls for other properties too
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句