C# .NET - Validation to javascript - Asked By Jahir on 23-Mar-12 12:51 AM

Earn up to 10 extra points for answering this tough question.
This is my RegularExpressionValidator :

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"

ControlToValidate="TextBox6" ErrorMessage="Please Enter in correct Format"

ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"

ValidationGroup="a"></asp:RegularExpressionValidator>

i want this coding in java script like below shown:
but this below shown coding is error...

<asp:Button ID="Button1" runat="server" Text="submit" OnClientClick="return Validate()" />

<script language="javascript" type="text/javascript">

function Validate()
{

var regVal="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* ";

if(document.getElementById('<%=TextBox1.ClientID%>').value.search(regVal) == -1)  //if match failed than show alert
{

alert("Please Enter Correct Format");
document.getElementById('<%=TextBox1.ClientID%>').focus();

return false;
}

}

</script>

Mihir Soni replied to Jahir on 23-Mar-12 01:42 PM
Hello,

You are trying to use some properties of textbox which are not exist.

Please modify your javascript code as following :-

function Validate(){
var regVal="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* ";
val txtValue = document.getElementById('<%=textbox1.ClientID%>').value
 if (RE_SSN.test(txtValue)) {
  alert("Valid");
 } else {
  alert("Not Valid");
 }
}

Hope this solves your problem.

Thanks
Abhinav Sejpal replied to Jahir on 24-Mar-12 04:10 AM
Hello,

Perfect solution for your problem. find the below snippet. :)


<asp:Button ID="Button1" runat="server" Text="submit" OnClientClick="return Validate()" />

<script language="javascript" type="text/javascript">

function Validate()
{

var emailPat = /^(?:\w+\.?)*\w+@(?:\w+\.)+\w+$/;
  
   var emailID=document.getElementById('<%=txtmailid.ClientID %>').value;
   if(emailID!="")
   {
   var matchArray=emailID.match(emailPat);
   if(matchArray==null)
   {
   alert("Please Enter Email Address in Correct Format..");
   document.getElementById('<%=txtmailid.ClientID %>').focus();
   return false;
   }
   }

}

</script>

[)ia6l0 iii replied to Jahir on 24-Mar-12 10:27 AM
There are lots of mistakes in your code and couple of mistakes in the suggestions from the above members. 

I have listed them below. 

a) You have to return the valdiation result (true/false) mandatorily based on the value entered, because you have the OnClientClick function expecting a result.
OnClientClick="return Validate()" 

So the validate function needs to return true/false. b) You have used the RegEx in the wrong manner. You need to place the slashes before and after the regular expression. See the code sample below. c) When you want to test a Regular Expression , you use the "Test" method of the RegEx object. See the following modified code snippet with changes.
function Validate() So the validate function needs to return true/false.
{
	var regVal=/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
	if (!regVal.test(document.getElementById('<%=TextBox1.ClientID%>').value))  
	//if match failed than show alert
	{
		alert("Please Enter Correct Format");
	      document.getElementById('<%=TextBox1.ClientID%>').focus();
		return false;
	}
	//Add a return true otherwise
	return true;
}

Hope this helps.
Somesh Yadav replied to Jahir on 26-Mar-12 01:16 AM
Hi kiran refer to the below link,

http://www.9lessons.info/2009/03/perfect-javascript-form-validation.html

Hope it helps you.