Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/477.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用正则表达式验证用户输入以获取字符和数字无效_Javascript_Regex_Asp.net Mvc_Validation - Fatal编程技术网

Javascript 使用正则表达式验证用户输入以获取字符和数字无效

Javascript 使用正则表达式验证用户输入以获取字符和数字无效,javascript,regex,asp.net-mvc,validation,Javascript,Regex,Asp.net Mvc,Validation,我正在对用户登录输入进行验证,我希望类似于ab12123。所以我用javascript验证它,以便在单击按钮时进行验证。如果无效,则应显示一条消息。问题是函数不起作用,我打开开发工具检查javascript,没有错误。 观点: @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal"> <hr /> @Html.Va

我正在对用户登录输入进行验证,我希望类似于ab12123。所以我用javascript验证它,以便在单击按钮时进行验证。如果无效,则应显示一条消息。问题是函数不起作用,我打开开发工具检查javascript,没有错误。 观点:

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">

        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            <label class="control-label col-md-2">
                User Login <span class="required" id="star">*</span>
            </label>
            <div class="col-md-4">

                @Html.TextBox("UserLogin", null, new { @class = "form-control", required = "required" , id= "UserLogin" })

                <div class="col-lg-3" id="checkID" hidden>
                    <label style="color:red">Please enter the ID in the Proper Format Numbers only EX:kl12123</label>
                </div>
            </div>
        </div>

        <div class="form-group">
            <label class="control-label col-md-2">
                Username <span class="required" id="star">*</span>
            </label>
            <div class="col-md-4">
                @Html.TextBox("UserName" , null, new { @class = "form-control", required = "required", id = "UserName" } )

            </div>
        </div>

        <div class="form-group">
            <label class="control-label col-md-2">
                Groups <span class="required" id="star">*</span>
            </label>
            <div class="col-md-4">
                @Html.DropDownList("GroupID", null, htmlAttributes: new { @class = "form-control", required = "required" , id= "GroupID" })
                @Html.ValidationMessageFor(model => model.GroupID, "", new { @class = "text-danger" })
            </div>
        </div>



        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="button" class="btn btn-success" value="ValidateMe" onclick="ValidateMe()" />
            </div>
        </div>
    </div>
}
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()

@Html.ValidationSummary(true,“,new{@class=“text danger”}) 用户登录* @TextBox(“UserLogin”,null,new{@class=“form control”,required=“required”,id=“UserLogin”}) 请以正确格式输入ID,仅限编号EX:kl12123 用户名* @TextBox(“UserName”,null,新{@class=“form control”,required=“required”,id=“UserName”}) 团体* @DropDownList(“GroupID”,null,htmlAttributes:new{@class=“form control”,required=“required”,id=“GroupID”}) @Html.ValidationMessageFor(model=>model.GroupID,“,new{@class=“text danger”}) }
javascript:

 function ValidateMe() {
        //$('#GroupID').on('change', function () {

        var IDValid = false;
        var Login = $('#UserLogin').val();
        var name = $('#UserName').val();
        var GroupId = $('#GroupID').val();

            if (Login.length < 6) {
                $('#checkID').show();
                IDValid = false;
            }
            else {
                var regexnumber = new RegExp(/(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,}/);
                if (regexnumber.test(Login)) {
                    $('#checkID').hide();
                    IDValid = true;
                }
                else {
                    $('#checkID').show();
                    IDValid = false;
                }

            }
            if (IDValid == true) {
                var data = {
                    UserLogin: Login,
                    UserName: name,
                    UserGroup: GroupId
                };
                $.ajax({
                    type: 'POST',
                    url: '/Account/SaveUser',
                    data: data

                });
            }
    }
函数ValidateMe(){
//$('#GroupID')。on('change',function(){
var IDValid=false;
var Login=$('#UserLogin').val();
var name=$('#UserName').val();
var GroupId=$('#GroupId').val();
如果(Login.length<6){
$('#checkID').show();
IDValid=false;
}
否则{
var regexnumber=new RegExp(/(?=.*\d)(?=.[a-z])(?=.[a-z])。{6,}/);
if(regexnumber.test(登录)){
$('#checkID').hide();
IDValid=true;
}
否则{
$('#checkID').show();
IDValid=false;
}
}
if(IDValid==true){
风险值数据={
UserLogin:Login,
用户名:name,
用户组:GroupId
};
$.ajax({
键入:“POST”,
url:“/Account/SaveUser”,
数据:数据
});
}
}
我感谢你在这件事上的帮助。
谢谢。

MVC可以使用jQuery validate自动处理客户端验证,这应该是预先设置的。它使用模型上的属性来设置验证要求,并使用
Html.helper作为模型绑定的扩展。您应该在
App\u Start/BundleConfig.cs
中看到这一行,确认它已安装:

bundles.Add(新脚本包(“~/bundles/jqueryval”)。包括(
“~/Scripts/jquery.validate*”);
如果没有,您可以解压并安装到
Scripts
目录中,然后将上面的行添加到
BundleConfig.cs

您可以这样使用此方法:

型号

使用System.ComponentModel.DataAnnotations;
公共类用户
{
[必需]
[RegularExpression(@“(?=.*\d)(?=.[a-z])(?=.[a-z])。{6,}”,ErrorMessage=“请以正确格式输入ID,仅限数字EX:kl12123”)]
公共字符串UserLogin{get;set;}
[必需]
公共字符串用户名{get;set;}
[必需]
公共int用户组
}
查看

@model Namespace.User
//将@Html.TextBox()帮助程序替换为以下内容:
@Html.TextBoxFor(model=>model.UserLogin,新的{@class=“form control”})
@Html.ValidationMessageFor(model=>model.UserLogin,“,new{@class=“text danger”})
@Html.TextBoxFor(model=>model.UserName,新的{@class=“form control”})
@Html.ValidationMessageFor(model=>model.UserName,“,new{@class=“text danger”})
@Html.DropDownListFor(model=>model.UserGroup,null,new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.UserGroup,“,new{@class=“text danger”})
//将以下内容放在视图的最底部:
@节脚本
{
@Scripts.Render(“~/bundles/jqueryval”)
}
这给了我们:


正则表达式看起来是正确的,尽管我会使用不带构造函数的正则表达式文本,
var regexnumber=/^(?=.*\d)(?=.[a-z])(?=.[a-z])。{6,}/
。如果它不起作用,问题是其他代码。谢谢你的建议。我确实让我的javascript工作了,但使用该模型更干净,代码更少。顺便说一下,对我起作用的正则表达式是“^[a-z]{2}[0-9]{5,}$。非常感谢