سلام
من کد زیر را نوشته ام
@model jqueryvalidation.Models.User @{ ViewBag.Title = "Create"; } <h2>Create</h2> <script> function validate() { var bool = true; var emails = document.getElementById('emailsp'); var email = document.getElementById('email').value; var pattern = /^[\w.]+@[a-z0-9-]+\.[a-z]{2,6}/i; if (!pattern.test(email)) { emails.innerText = 'ایمیل را صحیح وارد نمایید'; bool = false; } return bool; } </script> @*@using (Html.BeginForm())*@ @using (Html.BeginForm(new { onsubmit = "return validate()" })) { @Html.AntiForgeryToken() <div class="form-horizontal"> <h4>User</h4> <hr /> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) <div class="form-group"> @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Family, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Family, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Family, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.NationalId, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.NationalId, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.NationalId, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Age, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Age, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Age, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control",Id="email" } }) @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" }) <span id="emailsp" style="color: red"></span> </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.ConfirmPassword, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.ConfirmPassword, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.ConfirmPassword, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Create" class="btn btn-default" /> </div> </div> </div> } <div> @Html.ActionLink("Back to List", "Index") </div> <script src="~/Scripts/jquery-1.10.2.min.js"></script> <script src="~/Scripts/jquery.validate.min.js"></script> <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
اما هنگام اجرا از خط
var pattern = /^[\w.]+@[a-z0-9-]+\.[a-z]{2,6}/i;
ارور میگیرد
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.
Parser Error Message: "[" is not valid at the start of a code block. Only identifiers, keywords, comments, "(" and "{" are valid.
Source Error:
مشکل چگونه حل میشود
این طوری باید استفاده کنید
@Html.Editor("country_code", null, new { htmlAttributes = new { @class = "form-control" , required = "required" , pattern="[A-Za-z]{3}"} })
سلام
استاد ایا این خط از کد ایراد دارد
@using (Html.BeginForm(new { onsubmit = "return validate();" }))
چون وقتی برنامه را اجرا میکنم اصلا تابع مربوطه اجرا نمیشود در صورتیه تابع را در کلیک input مینویسم درست کارمیکند
<input type="submit" value="Create" class="btn btn-default" onclick = "return validate();" />
علت کار نکرد کد بالا چیست؟
باتشکر
به شکل زیر امتحان کنید
@using (Html.BeginForm("Home","Index",FormMethod.Post,new { onsubmit = "return validate();" }))
هیچ کاربری تا کنون از این پست تشکر نکرده است
با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)