After research in the Internet I found finally the solution, here's the answer and the credits belong to him.here's the answer and the credits belong to him.
I'm not sure why the WebPart
couldn't or can't detect the ASP.NET
controls in var
variables maybe there's some about the if
conditionals...
Here's the full validation code that I'm using in the WebPart
that I develop a few days ago.
<script type="text/javascript">
function validateForm() {
//Declare variables.
var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
/*For some unknown reason, fields to validate (if they're declared on variables type "var") are NOT detected.*/
//Validate fields (ASP.NET fields).
if (document.getElementById('<%= txtSubject.ClientID %>').value.length == 0) {
alert("Field ''Subject'' is required.");
document.getElementById('<%= txtAsunto.ClientID %>').focus();
return false;
} else if (document.getElementById('<%= txtEmail.ClientID %>').value.length == 0) {
alert("Field ''Email'' is required.");
document.getElementById('<%= txtEmail.ClientID %>').focus();
return false;
}
else {
if (!emailPattern.test(document.getElementById('<%= txtEmail.ClientID %>').value)) {
alert("Field ''Email'' is invalid.");
document.getElementById('<%= txtEmail.ClientID %>').focus();
return false;
}
else{
//Your code...
}
}
}</script>
N.B: The following code was tested and it works in pure HTML
, SharePoint 2010
and SharePoint 2013 (Online)
.