PDA

View Full Version : سوال: تعریف pattern در یک view



Iran58
پنج شنبه 21 اردیبهشت 1396, 11:52 صبح
سلام
من کد زیر را نوشته ام


@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.

مشکل چگونه حل میشود

alireza_s_84
پنج شنبه 21 اردیبهشت 1396, 13:13 عصر
علامت @ برای Razor دارای معنا هست شما هرجا توی View میخواین این از این علامت استفاده کنید باید اونو به شکل @@ وارد کنید تا از حالت نگارش کدهای Razor متمایز بشه.
پترن شما باید به شکل زیر باشه:
var pattern = /^[\w.]+@@[a-z0-9-]+\.[a-z]{2,6}/i;

Iran58
شنبه 23 اردیبهشت 1396, 08:51 صبح
سلام
استاد ایا این خط از کد ایراد دارد
@using (Html.BeginForm(new { onsubmit = "return validate();" }))
چون وقتی برنامه را اجرا میکنم اصلا تابع مربوطه اجرا نمیشود در صورتیه تابع را در کلیک input مینویسم درست کارمیکند
<input type="submit" value="Create" class="btn btn-default" onclick = "return validate();" />
علت کار نکرد کد بالا چیست؟
باتشکر

alireza_s_84
شنبه 23 اردیبهشت 1396, 10:35 صبح
سلام
استاد ایا این خط از کد ایراد دارد
@using (Html.BeginForm(new { onsubmit = "return validate();" }))
چون وقتی برنامه را اجرا میکنم اصلا تابع مربوطه اجرا نمیشود در صورتیه تابع را در کلیک input مینویسم درست کارمیکند
<input type="submit" value="Create" class="btn btn-default" onclick = "return validate();" />
علت کار نکرد کد بالا چیست؟
باتشکر
ببینید html تولید شده برای فرم حاوی onsubmit = "return validate();" هست یا خیر؟