Javascript Validation using regexp

Name:
Alphabets, numbers and space(' ') no special characters min 3 and max 20 characters.
var ck_name = /^[A-Za-z0-9 ]{3,20}$/;

Email
Standard email address
var ck_email = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i

UserId
Supports alphabets and numbers no special characters except underscore('_') min 3 and max 20 characters.
var ck_username = /^[A-Za-z0-9_]{3,20}$/;

Password
Password supports special characters and here min length 6 max 20 charters.
var ck_password = /^[A-Za-z0-9!@#$%^&*()_]{6,20}$/;

 Script to validate the special characters

<script language="JavaScript 1.2" type="text/JavaScript">
<!--


function validate() {
if (document.form1.folder_id.value == "") {
alert ("Please enter your Event title");
document.form1.folder_id.focus();
return false;
}
var string = document.form1.folder_id.value;
if (!string) {
alert ("Event title missing !");
return false;
}
var iChars = "*|,\":<>[]{}`\';()@&$#%";
for (var i = 0; i < string.length; i++) {
if (iChars.indexOf(string.charAt(i)) != -1){
alert ("'Event title' contains illegal characters!");
document.form1.folder_id.focus();
return false;
}
else if (document.form1.numOfUpload.selectedIndex == 0) {
alert ("Please select number of files to upload!");
return false;
}
}
return true;
}

</script>



JavaScript Code:
You have to include this code within the tag HEAD of the page.

<script type="text/javascript">
var ck_name = /^[A-Za-z0-9 ]{3,20}$/;
var ck_email = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]
{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i 
var ck_username = /^[A-Za-z0-9_]{1,20}$/;
var ck_password =  /^[A-Za-z0-9!@#$%^&*()_]{6,20}$/;

function validate(form){
var name = form.name.value;
var email = form.email.value;
var username = form.username.value;
var password = form.password.value;
var gender = form.gender.value;
var errors = [];
 
 if (!ck_name.test(name)) {
  errors[errors.length] = "You valid Name .";
 }
 if (!ck_email.test(email)) {
  errors[errors.length] = "You must enter a valid email 
address.";
 }
 if (!ck_username.test(username)) {
  errors[errors.length] = "You valid UserName no special 
char .";
 }
 if (!ck_password.test(password)) {
  errors[errors.length] = "You must enter a valid Password ";
 }
 if (gender==0) {
  errors[errors.length] = "Select Gender";
 }
 if (errors.length > 0) {

  reportErrors(errors);
  return false;
 }
  return true;
}
function reportErrors(errors){
 var msg = "Please Enter Valide Data...\n";
 for (var i = 0; i<errors.length; i++) {
 var numError = i + 1;
  msg += "\n" + numError + ". " + errors[i];
}
 alert(msg);
}
</script>

HTML Code
You have to modify action="#"
<form method="post" action="#" onSubmit="return validate(this);" name="form">

</form> 

 For url

<html>
<head>
<title>Url validation using regex</title>
<script type="text/javascript">
    function validate() {
        var url = document.getElementById("url").value;
        var pattern = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
        if (pattern.test(url)) {
            alert("Url is valid");
            return true;
        } 
            alert("Url is not valid!");
            return false;

    }
</script>
</head>
<body>
<h2>Validating Url..</h2>
Url :<input type="text" name="url" id="url" />
<input type="submit" value="Check" onclick="validate();" />
</body>
</html>

url pattern is like HYPERLINK

"http://www.roseindia.net/hibernate/examples/criteria"

http://www.roseindia.net/hibernate/examples/criteria


In pattern = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!-\/]))?/;

/../(forward slashes) is used to quote your regular expression.

() is used to group the content.

| is used for or condition

\/ backslash consider / as a literal .

\w+ for one or more alphanumeric value.

{0,1} shows the range of content having length between 0 to 1.

* matches the preceding character zero or more time.

[..] matches any letter enclosed with in.

+ shows that preceding character come 1 or more times.

. Shows that \ will treat . as a literal.

$ is used for ending the string.

test() method takes one argument and check that to the pattern.