我是新来的asp.net mvc和我有一些验证的aform,我想验证它在客户端,所以我写在JavaScript中的正常脚本 要更多specefic我的问题是返回false(因为它不应该发送任何数据到服务器,仍然apage,但这不会发生),注意:我测试脚本在正常的HTML文件与JS和工作正常 ,但正如我所说我不熟悉MVC,所以我想知道是否有任何我错过了它的工作,如果有任何toturial在这specefic点这将是很好的,因为我注意到在这个地方(没有地方对于初学者 :); 这是我的代码片段太窗体验证在asp.net mvc(使用javascript)
@model registerationex.Models.register
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(false)
<fieldset>
<legend>register</legend>
<div class="editor-label">
@Html.LabelFor(model => model.name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.name)
<span id="error"></span>
@Html.ValidationMessageFor(model => model.name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.email)
</div>
<div class="editor-field">
<input class="text-box single-line" data-val="true" data-val-required="The email field is required." id="email" name="email" type="text" value="" onblur="checkuser(email);">
<span id="erroruser"></span>
@Html.ValidationMessageFor(model => model.email)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.age)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.age)
<span id="errorage"></span>
@Html.ValidationMessageFor(model => model.age)
</div>
<p>
<input type="submit" value="Create" onclick="allvalidate();" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
<script>
function allvalidate() {
var validated = true;
if (!validate()) validated = false;
if (!checkAge(age)) validated = false;
if (!checkuser(email)) validated = false;
return validated;
}
function validate() {
var txtf = document.getElementById('name');
if (txtf.value == 0 || txtf.value == null) {
document.getElementById('error').innerText = ('you must enter firstname');
document.getElementById('error').style.color = 'red';
txtf.focus();
return false;
}
else {
document.getElementById('error').innerText = ('');
return true;
}
}
function checkAge(input) {
if (input.value < 18 || input.value > 70) {
document.getElementById('errorage').innerText = ('age must be from 18 :70');
document.getElementById('errorage').style.color = 'red';
return false;
}
else {
document.getElementById('errorage').innerText = ('');
return true;
}
}
function checkuser(input) {
var pattern = '^[a-zA-Z]+$';
if (input.value.match(pattern)) {
document.getElementById('erroruser').innerText = '';
return true;
}
else {
document.getElementById('erroruser').innerText = ('enter valid email');
document.getElementById('erroruser').style.color = 'red';
return false;
}
}
</script>
感谢您的答案和反弹,我也只是想知道你的东西。我知道服务器验证不是可选的,所以我想添加一个客户端,除此之外,通过减少与服务器的连接来提高性能,所以如果我在模型中使用mvc属性(这是在服务器或客户端的验证工作)并感谢您的电子邮件模式通知,请注意,这只是了解验证,并且我想编写简单的模式来接受单词并将其命名为仅电子邮件@Stephen Muecke – user4833581
首先,如果您已包含必需的脚本('jquery- {版本} .js','jquery.validate.js'和'jquery.validate.unobtrusive.js'),并且没有禁用客户端验证,那么您将使用验证获得**客户端和服务器端验证属性。这是MVC的一个很好的特性,所以不要忽略它。 –
接下来,'@ Html.LabelFor(model => model.email)'将生成一个带有“email”文本的'