简体   繁体   English

Razer视图中的Javascript Regex电子邮件验证

[英]Javascript Regex Email Validation in Razer view

I am attempting to validate an email input that is created with an HTML helper class. 我正在尝试验证使用HTML helper类创建的电子邮件输入。 The 'validateForm' method is being called properly everytime, but the if statement keeps failing. 每次都会正确调用'validateForm'方法,但是if语句一直失败。 My two best theories are that my Regex, that I found here , is off due to me having to escape the '@' characters, or I'm not properly comparing it. 我的两个最佳理论是,我在这里找到的Regex已关闭,因为我必须转义'@'字符,否则我无法正确比较它。

Thanks always! 永远谢谢!

Javascript: Javascript:

function validateForm() {
    var isValid = true;
    var errorMessage = "";
    var emailRegExp = /^(([^<>()[\]\\.,;:\s@@\"]+(\.[^<>()[\]\\.,;:\s@@\"]+)*)|(\".+\"))@@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    var x = document.forms["Update"]["complaint.Responsible"].value;

    if (x === null || x === "" || !emailRegExp.test(x)) {
        errorMessage += "A valid 'Responsible' was not entered<br/>";
        document.getElementById("Responsible").style.borderColor = '#F00';
        alert("No! >:(");
        isValid = false;
    }

    return isValid;
}

Form CSHTML: 表格CSHTML:

@Html.TextBoxFor(model => Model.complaint.Responsible, new { @Class = "txtLong" })

Form HTML: 表单HTML:

<form name="Update" onsubmit="return validateForm()" method="post" id="formBody"  enctype="multipart/form-data">
     <input class="txtLong" id="complaint_Responsible" name="complaint.Responsible" type="text" value="sdbsv rszaer gse er">

Update: 更新:

This is why I can't use a single '@' symbol 这就是为什么我不能使用单个'@'符号的原因 在此处输入图片说明

(one line - as is in code) (一行-如代码所示)

在此处输入图片说明

The @@ is telling the regex engine that you expect two @ symbols. @@告诉正则表达式引擎,您需要两个@符号。 Replace it with the pattern below: 将其替换为以下模式:

var emailRegExp = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM