Email Validation: Cross browser Email Validation in JS. JavaScript for Email Address Validation with explanation of the code.The script on this page has been updated to deal with cross-browser issues. Learn how to validate a users email address using Javascript.
Using Regular Expressions is probably the best way. Here's is an function Which will return true if Email is a Valid email and false if email is not valid .
<script>
function
validateEmail(email) {
var regstring
= /^(([^<>()[\]\\.,;:\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,}))$/;
return regstring.test(email);
}
</script>
But keep in mind that one should not rely only upon JavaScript validation. JavaScript can easily be disabled. This should be validated on the server side as well.
The c# regex.match method is typically used to validate a string or to ensure that a string conforms to a particular pattern without retrieving that string for subsequent manipulation.Below is a string extension method that uses c# regex matches to check if the string is alphanumeric.It will return true if given string contain only number and alphabets.
using System;
using
System.Collections.Generic;
using
System.Text;
using
System.Text.RegularExpressions;
public static class CheckString
{
public static bool
IsAlphanumeric(string source)
{
Regex
pattern = new Regex("[^0-9a-zA-Z]");
return
!pattern.IsMatch(source);
}
}
// THE Below
Example USe Code.
class Program
{
static void Main(string[]
args)
{
string
testString = Console.ReadLine();
if
(CheckString.IsAlphanumeric(testString))
Console.WriteLine("Yes string is Alphanumeric!");
else
Console.WriteLine("No string is not Alphanumeric!");
Console.ReadKey();
}
}
some time we require Only numbers can be enter by user into a Textbox for that We can use Regular expression validator in asp.net for this:
In the validation expression property keep ^\d+$.
Example:
In the validation expression property keep ^\d+$.
Example:
<asp:TextBox ID="txtnumber"
runat="server"
ValidationGroup="number"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
runat="server"
ControlToValidate="txtnumber"
ErrorMessage="Please Enter Only
Numbers" ValidationExpression="^\d+$" ValidationGroup="number"></asp:RegularExpressionValidator>
A regular expression known as regex in short is a special text string which describes a search pattern.
Here is a c# function which accept a string as input parameter and return true if string is valid Email address and return false if string is not valid .
For this function to work properly you have to add these namespaces.
using System.Text;
using System.Text.RegularExpressions;
protected bool checkEmail(string Emailtext)
{
Regex MyEmailRegex = new Regex(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
if (string.IsNullOrEmpty(Emailtext))
{
return false;
}
else
{
return MyEmailRegex.IsMatch(Emailtext);
}
}