我怎样才能prevent用户点击按钮提交表单如果指定域是无效的? [英] How can i prevent User click the button to submit form if specify field is not valid?
问题描述
我有一个jQuery函数在我看来,以检查 UsernameTextbox
的有效数据。我想prevent用户对注册点击
按钮,直到本场有效。
I have a jquery function to check valid data on UsernameTextbox
in my View. I want to prevent User click on the Register
button until this field valid.
禁用按钮的最佳方法是什么?我只是想,如果值是无效的,用户点击按钮,只专注于 UsernameTextbox
申请?
Disable button is it the best method? I just want when the value is not valid, user click on button just focus to the UsernameTextbox
filed?
更新code:
下面是我的模型:
[Required]
[Remote("CheckUsername", "Account", ErrorMessage = "Username already exits.")]
public string Username { get; set; }
和使用GET方法控制器:
and Controller with GET method:
[HttpGet]
public JsonResult CheckUsername(string userName)
{
var user = IUserRepo.GetUserByUrName(userName);
bool isValid = true;
if (user!=null)
{
isValid = false;
}
return Json(isValid, JsonRequestBehavior.AllowGet);
}
在我看来:
@using (Ajax.BeginForm("Register","Account",new {area = "Area"},null))
{
@Html.ValidationSummary(true)
<table>
<tbody>
<tr>
<td class="info_label">Tên đăng nhập</td>
<td>@Html.EditorFor(m => m.User.Username)
</td>
<td class="check_user">@Html.ValidationMessageFor(m => m.User.Username)</td>
</tr>
<tr> ........
为什么没有错误信息出现?我想有效的中等当用户填写的数据或将文本框喜欢这个网站 http://yame.vn/TaiKhoan/DangKy 。
推荐答案
注意:以下提到的建议是只为MVC3及以上
Note : The below mentioned suggestion is only for MVC3 and above
路飞,你可以删除AJAX调用来检查用户名存在
Luffy, you can remove the Ajax Call to check UserName existence
型号
public class UserModel
{
// Remote validation is new in MVC3. Although this will also generate AJAX
// call but, you don't need to explicitly type the code for Ajax call to
// check the User Existence. Remote Validation will take care of it.
[Required]
[Remote("CheckUsername", "Account", ErrorMessage = "User Already Exist")]
public string UserName { get; set; }
}
控制器
[HttpGet]
public JsonResult CheckUsername(string MyProp)
{
// Your Validation to check user goes here
bool isValid = true;
return Json(isValid, JsonRequestBehavior.AllowGet);
//Note - This will be called whenever you post the form.
//This function will execute on priority, after then the Index
//Post Action Method.
}
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(UserModel model)
{
// This action method will execute if the UserName does not exists
// in the DataBase
return View(model);
}
查看
@using (Ajax.BeginForm("Action", "Controller", new { area = "Area" }, null))
{
@Html.TextBoxFor(i => i.UserName);
<input type="submit" name="Submit" value="Submit" />
// Whenever you submit the form, the control will go directly to
// CheckUsername function. In case the UserName doesn't exists only
// then the Post action method will be executed.
}
脚本
<script src="jquery-1.7.1.min.js" type="text/javascript"></script>
<script src="jquery.validate.min.js" type="text/javascript"></script>
<script src="jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
这篇关于我怎样才能prevent用户点击按钮提交表单如果指定域是无效的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!