I am attempting to validate an email input that is created with an HTML helper class. The 'validateForm' method is being called properly everytime, but the if statement keeps failing. 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.
Thanks always!
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:
@Html.TextBoxFor(model => Model.complaint.Responsible, new { @Class = "txtLong" })
Form 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,}))$/;