function is_valid_username(str_username)
{
var filter = /^([a-zA-Z\s0-9]*)$/;
if (!filter.test(str_username))
{
alert(’Wrong username format.’);
}
else
{
alert('Username format is ok.')
}
return false;
}
using System.Text.RegularExpressions;
private void is_valid_username(string str_username)
{
Regex matchRegex = new Regex(@"^[a-zA-Z0-9_]{3,16}$");
MatchCollection matches = matchRegex.Matches(str_username);
if(matches.Count==0)
{
Response.Write("Wrong username format."); // In Webserver
//MessageBox.Show("Wrong username format."); //In Windows Form
}
else
{
Response.Write("Username format is ok."); // In Webserver
//MessageBox.Show("Username format is ok."); //In Windows Form
}
}
|
Imports System.Text.RegularExpressions
Private Sub is_valid_username(ByVal str_username As String)
Dim matchRegex As New Regex("^[a-zA-Z0-9_]{3,16}$")
Dim matches As MatchCollection = matchRegex.Matches(str_username)
If matches.Count = 0 Then
Response.Write("Wrong username format.") ' In Webserver
'MessageBox.Show("Wrong username format."); 'In Windows Form
Else
Response.Write("Username format is ok.") ' In Webserver
'MessageBox.Show("Username format is ok."); 'In Windows Form
End If
End Sub
|
import java.util.regex.*;
public class is_valid_username
{
public static void main(String str_username)
{
Pattern p=Pattern.compile("^[A-Za-z0-9]+$");
Matcher m=p.matcher(str_username);
boolean matchFound = m.matches();
if(matchFound)
{
System.out.println("Username format is ok.");
}
else
{
System.out.println("Wrong username format.");
}
}
}
|
Function is_valid_username($str_username)
{
if (preg_match('/^[a-z\d_]{5,20}$/i',$str_username))
{
echo "Username format is ok.";
}
else
{
echo "Wrong username format.";
}
}
|
Validate email addresses : example@domain.com
function is_valid_email(str_email_id)
{
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (!filter.test(str_email_id))
{
alert(’Wrong email address format.’);
}
else
{
alert('Email address format is ok.')
}
return false;
}
|
using System.Text.RegularExpressions;
private void is_valid_email(string email_id)
{
Regex matchRegex = new Regex(@"[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9_\-\.]+\.[a-zA-Z]{2,5}");
MatchCollection matches = matchRegex.Matches(email_id);
if(matches.Count==0)
{
Response.Write("Wrong email address format."); // In Webserver
//MessageBox.Show("Wrong email address format."); //In Windows Form
}
else
{
Response.Write("Email address format is ok."); // In Webserver
//MessageBox.Show("Email address format is ok."); //In Windows Form
}
}
|
Imports System.Text.RegularExpressions
Private Sub is_valid_email(ByVal email_id As String)
Dim matchRegex As New Regex("[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9_\-\.]+\.[a-zA-Z]{2,5}")
Dim matches As MatchCollection = matchRegex.Matches(email_id)
If matches.Count = 0 Then
Response.Write("Wrong email address format.") ' In Webserver
'MessageBox.Show("Wrong email address format."); 'In Windows Form
Else
Response.Write("Email address format is ok.") ' In Webserver
'MessageBox.Show("Email address format is ok."); 'In Windows Form
End If
End Sub
|
import java.util.regex.*;
public class is_valid_email
{
public static void main(String str_email_id)
{
Pattern p=Pattern.compile("[a-zA-Z]*[0-9]*@[a-zA-Z]*.[a-zA-Z]*");
Matcher m=p.matcher(str_email_id);
boolean matchFound = m.matches();
if(matchFound)
{
System.out.println("Email address format is ok.");
}
else
{
System.out.println("Wrong email address format.");
}
}
}
|
Function is_valid_email($email_id)
{
if (preg_match('/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/',$email_id))
{
echo "Email address format is ok.";
}
else
{
echo "Wrong email address format.";
}
}
|
Validate Phone numbers: Validate US phone number eg: 123-456-7890
function is_valid_phone_no(str_phone_no)
{
var filter = /^((\+?1-)?\d\d\d-)?\d\d\d-\d\d\d\d$/;
if (!filter.test(str_phone_no))
{
alert(’Wrong phone number format.’);
}
else
{
alert('Phone number format is ok.')
}
return false;
}
|
using System.Text.RegularExpressions;
private void is_valid_phone_no(string str_phone_no)
{
Regex matchRegex = new Regex(@"\d{3}-\d{3}-\d{4}$");
MatchCollection matches = matchRegex.Matches(str_phone_no);
if(matches.Count==0)
{
Response.Write("Wrong phone number format."); // In Webserver
//MessageBox.Show("Wrong phone number format."); //In Windows Form
}
else
{
Response.Write("Phone number format is ok."); // In Webserver
//MessageBox.Show("Phone number format is ok."); //In Windows Form
}
}
|
Imports System.Text.RegularExpressions
Private Sub is_valid_phone_no(ByVal str_phone_no As String)
Dim matchRegex As New Regex("\d{3}-\d{3}-\d{4}$")
Dim matches As MatchCollection = matchRegex.Matches(str_phone_no)
If matches.Count = 0 Then
Response.Write("Wrong phone number format.") ' In Webserver
'MessageBox.Show("Wrong phone number format."); 'In Windows Form
Else
Response.Write("Phone number format is ok.") ' In Webserver
'MessageBox.Show("Phone number format is ok."); 'In Windows Form
End If
End Sub
|
import java.util.regex.*;
public class is_valid_phone_no
{
public static void main(String str_phone_no)
{
Pattern p=Pattern.compile("^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{4})$");
Matcher m=p.matcher(str_phone_no);
boolean matchFound = m.matches();
if(matchFound)
{
System.out.println("Phone number format is ok.");
}
else
{
System.out.println("Wrong phone number format.");
}
}
}
|
Function is_valid_phone_no($str_phone_no)
{
if (preg_match('/\(?\d{3}\)?[-\s.]?\d{3}[-\s.]\d{4}/x',$str_phone_no))
{
echo "Phone number format is ok.";
}
else
{
echo "Wrong phone number format.";
}
}
|
Validate Zip/Postal Codes: Validate US Zip/Postal Code eg: 12345-6789
function is_valid_zip_code(str_zip_code)
{
var filter = /^\d\d\d\d\d-\d\d\d\d$/;
if (!filter.test(str_zip_code))
{
alert(’Wrong Zip/Postal Code format.’);
}
else
{
alert('Zip/Postal Code format is ok.')
}
return false;
}
|
using System.Text.RegularExpressions;
private void is_valid_zip_code(string str_zip_code)
{
Regex matchRegex = new Regex(@"\d{5}-\d{4}$");
MatchCollection matches = matchRegex.Matches(str_zip_code);
if(matches.Count==0)
{
Response.Write("Wrong Zip/Postal Code format."); // In Webserver
//MessageBox.Show("Wrong Zip/Postal Code format."); //In Windows Form
}
else
{
Response.Write("Zip/Postal Code format is ok."); // In Webserver
//MessageBox.Show("Zip/Postal Code format is ok."); //In Windows Form
}
}
|
Imports System.Text.RegularExpressions
Private Sub is_valid_zip_code(ByVal str_zip_code As String)
Dim matchRegex As New Regex("\d{5}-\d{4}$")
Dim matches As MatchCollection = matchRegex.Matches(str_zip_code)
If matches.Count = 0 Then
Response.Write("Wrong Zip/Postal Code format.") ' In Webserver
'MessageBox.Show("Wrong Zip/Postal Code format."); 'In Windows Form
Else
Response.Write("Zip/Postal Code format is ok.") ' In Webserver
'MessageBox.Show("Zip/Postal Code format is ok."); 'In Windows Form
End If
End Sub
|
import java.util.regex.*;
public class is_valid_zip_code
{
public static void main(String str_zip_code)
{
Pattern p=Pattern.compile("^(\\d{5})[- ]?(\\d{4})$");
Matcher m=p.matcher(str_zip_code);
boolean matchFound = m.matches();
if(matchFound)
{
System.out.println("Zip/Postal Code format is ok.");
}
else
{
System.out.println("Wrong Zip/Postal Code format.");
}
}
}
|
Function is_valid_zip_code($str_zip_code)
{
if (preg_match('/^([0-9]{5})(-[0-9]{4})?$/i',$str_zip_code))
{
echo "Zip/Postal Code format is ok.";
}
else
{
echo "Wrong Zip/Postal Code format.";
}
}
|
Validate Domain Name eg: http://live.com
function is_valid_url(str_url)
{
var filter = /^(ht|f)tps?:\/\/[a-z0-9-\.]+\.[a-z]{2,4}\/?([^\s<>\#%"\,\{\}\\|\\\^\[\]`]+)?$/
if (!filter.test(str_url))
{
alert(’Wrong URL format.’);
}
else
{
alert('URL format is ok.')
}
return false;
}
|
using System.Text.RegularExpressions;
private void is_valid_url(string str_url)
{
Regex matchRegex = new Regex(@"(http|https)://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?");
MatchCollection matches = matchRegex.Matches(str_url);
if(matches.Count==0)
{
Response.Write("Wrong URL format."); // In Webserver
//MessageBox.Show("Wrong URL format."); //In Windows Form
}
else
{
Response.Write("URL format is ok."); // In Webserver
//MessageBox.Show("URL format is ok."); //In Windows Form
}
}
|
Imports System.Text.RegularExpressions
Private Sub is_valid_url(ByVal str_url As String)
Dim matchRegex As New Regex("(http|https)://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?")
Dim matches As MatchCollection = matchRegex.Matches(str_url)
If matches.Count = 0 Then
Response.Write("Wrong URL format.") ' In Webserver
'MessageBox.Show("Wrong URL format."); 'In Windows Form
Else
Response.Write("URL format is ok.") ' In Webserver
'MessageBox.Show("URL format is ok."); 'In Windows Form
End If
End Sub
|
import java.util.regex.*;
public class is_valid_url
{
public static void main(String str_url)
{
Pattern p=Pattern.compile("(http|https)://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?");
Matcher m=p.matcher(str_url);
boolean matchFound = m.matches();
if(matchFound)
{
System.out.println("URL format is ok.");
}
else
{
System.out.println("Wrong URL format.");
}
}
}
|
Function is_valid_url($str_url)
{
if (preg_match('/^([0-9]{5})(-[0-9]{4})?$/i',$str_url))
{
echo "URL format is ok.";
}
else
{
echo "Wrong URL format.";
}
}