"JavaScript
is an object-oriented scripting language used to enable programmatic access
to objects within both the client application and other applications (or)
Client side Webpage validation."
JavaScript is the most popular scripting language on the internet, and works
in all major browsers, such as Internet Explorer, Firefox, Chrome, Opera, and
Safari.
Before you continue you should have a basic understanding of the following:
» HTML
If you want to study these subjects first learnHTML.
What is JavaScript?
» JavaScript was
designed to add interactivity to HTML pages. » JavaScript is a
scripting language. » A scripting
language is a lightweight programming language. » JavaScript is
usually embedded directly into HTML pages. » JavaScript is
an interpreted language (means that scripts execute without preliminary
compilation). » Everyone can
use JavaScript without purchasing a license.
The example below shows how to
use JavaScript to write text on a web page:
JavaScripts in a page will be executed immediately while the page loads into
the browser. This is not always what we want. Sometimes we want to execute a
script when a page loads, or at a later event, such as when a user clicks a
button. When this is the case we put the script inside a function, you will
learn about functions in a later chapter.
Scripts in <head>
Scripts to be executed when they are called, or when an event is triggered,
are placed in functions.
Put your functions in the head section, this way they are all
in one place, and they do not interfere with page content.
Example
<html>
<head> <script
type="text/javascript"> function message() { alert("This alert box was called with the onload
event"); } </script>
</head>
<body onload="message()">
</body>
</html>
Scripts <body>
If you don't want your script to be placed inside a function, or if your
script should write page content, it should be placed in the body section.
Example
<html>
<head>
</head>
<body onload="message()"> <script
type="text/javascript"> document.write("This message is written by
JavaScript"); </script>
</body>
</html>
Scripts<head> and <body>
You can place an unlimited number of scripts in your document, so you can
have scripts in both the body and the head section.
Example
<html>
<head> <script
type="text/javascript"> function message() { alert("This alert box was called with the onload
event"); } </script>
</head>
<body onload="message()"> <script
type="text/javascript"> document.write("This message is written by
JavaScript"); </script>
</body>
</html>
USERNAME VALIDATION USING
JAVASCRIPT
Username validation:
Example
<html>
<head> <script
type="text/javascript"> function validation() { var a = document.form.name.value; if(a=="") { alert("Please Enter Your Name"); document.form.name.focus(); return false; } } </script> </head>
<body>
<form name="form" method="post"
onsubmit="return validation()">
<tr>
<td> Your Name:</td>
<td><input type="text"
name="name""></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="sub"
value="Submit"></td>
</tr>
</form>
</body>
</html>
The Output is Shown Here:
UserName:
Username Validation allows
only Characters.
Example
<html>
<head> <script
type="text/javascript"> function validation() { var a = document.form.name.value; if(a=="") { alert("Please Enter Your Name"); document.form.name.focus(); return false; } if(!isNaN(a)) { alert("Please Enter Only Characters"); document.form.name.select(); return false; } } </script> </head>
<body>
<form name="form" method="post"
onsubmit="return validation()">
<tr>
<td> Your Name:</td>
<td><input type="text"
name="name""></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="sub"
value="Submit"></td>
</tr>
</form>
</body>
</html>
The Output is Shown Here:
UserName:
Username Validation allows
only Limited Characters.
Example
<html>
<head> <script
type="text/javascript"> function validation() { var a = document.form.name.value; if(a=="") { alert("Please Enter Your Name"); document.form.name.focus(); return false; } if(!isNaN(a)) { alert("Please Enter Only Characters"); document.form.name.select(); return false; } if ((a.length < 5) || (a.length > 15)) { alert("Your Character must be 5 to 15
Character"); document.form.name.select(); return false; } } </script> </head>
<body>
<form name="form" method="post"
onsubmit="return validation()">
<tr>
<td> Your Name:</td>
<td><input type="text"
name="name""></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="sub"
value="Submit"></td>
</tr>
</form>
</body>
</html>
The Output is Shown Here:
UserName:
PASSWORD VALIDATION USING JAVASCRIPT
Password validation:
Example
<html>
<head> <script
type="text/javascript"> function validation() { var a = document.form.pass.value; if(a=="") { alert("Please Enter Your Password"); document.form.pass.focus(); return false; } } </script> </head>
<body>
<form name="form" method="post"
onsubmit="return validation()">
<tr>
<td> password:</td>
<td><input type="text"
name="pass""></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="sub"
value="Submit"></td>
</tr>
</form>
</body>
</html>
The Output is Shown Here:
Password:
Password Validation (Hiding Your password)
Example
<html>
<head> <script
type="text/javascript"> function validation() { var a = document.form.pass.value; if(a=="") { alert("Please Enter Your Password"); document.form.pass.focus(); return false; } } </script> </head>
<body>
<form name="form" method="post"
onsubmit="return validation()">
<tr>
<td> Password:</td>
<td><input type="password"
name="pass""></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="sub"
value="Submit"></td>
</tr>
</form>
</body>
</html>
The Output is Shown Here:
Password:
Password Validation allows only Limited Characters or integers.
Example
<html>
<head> <script
type="text/javascript"> function validation() { var a = document.form.pass.value; if(a=="") { alert("Please Enter Your Password"); document.form.pass.focus(); return false; } if ((a.length < 4) || (a.length > 8)) { alert("Your Password must be 4 to 8
Character"); document.form.pass.select(); return false; } } </script> </head>
<body>
<form name="form" method="post"
onsubmit="return validation()">
<tr>
<td> Passsword:</td>
<td><input type="password"
name="pass""></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="sub"
value="Submit"></td>
</tr>
</form>
</body>
</html>
The Output is Shown Here:
Password:
EMAIL VALIDATION USING JAVASCRIPT
Email validation:
Example
<html>
<head> <script
type="text/javascript"> function validation() { var a = document.form.email.value; if(a=="") { alert("Please Enter Your Password"); document.form.email.focus(); return false; } } </script> </head>
<body>
<form name="form" method="post"
onsubmit="return validation()">
<tr>
<td> Email:</td>
<td><input type="text"
name="email""></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="sub"
value="Submit"></td>
</tr>
</form>
</body>
</html>
The Output is Shown Here:
Email-ID:
Email Validation (Email filter /@\ and /.\)
Example
<html>
<head> <script
type="text/javascript"> function ValidateContactForm() { var email = document.ContactForm.Email; if (email.value == "") { window.alert("Please enter a valid e-mail
address."); email.focus(); return false; } if (email.value.indexOf("@", 0) < 0) { window.alert("Please enter a valid e-mail
address."); email.focus(); return false; } if (email.value.indexOf(".", 0) < 0) { window.alert("Please enter a valid e-mail
address."); email.focus(); return false; } } </script> </head>
<body>
<form name="ContactForm" method="post"
onsubmit="return ValidateContactForm();">
<tr>
<td> Email:</td>
<td><input type="text"
name="Email""></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="sub"
value="Submit"></td>
</tr>
</form>
</body>
</html>
The Output is Shown Here:
Email:
Email Validation (Email filter /@\ and /.\)
Example
<html>
<head> <script
type="text/javascript"> function validation() { var
emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i var b=emailfilter.test(document.form2.mailid.value); if(b==false) { alert("Please Enter a valid Mail ID"); document.form2.mailid.focus(); return false; } } </script> </head>
<body>
<form name="form2" method="post"
onsubmit="return validat()">
<tr>
<td> Email-ID:</td>
<td><input type="text"
name="mailid"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="sub"
value="Submit"></td>
</tr>
</form>
</body>
</html>
The Output is Shown Here:
Email:
PHONE NUMBER VALIDATION USING JAVASCRIPT
Phone validation:
Example
<html>
<head> <script
type="text/javascript"> function validation() { var a = document.form.phone.value; if(a=="") { alert("Please Enter Your Phone Number"); document.form.phone.focus(); return false; } } </script> </head>
<body>
<form name="form" method="post"
onsubmit="return validation()">
<tr>
<td> Phone:</td>
<td><input type="text"
name="phone""></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="sub"
value="Submit"></td>
</tr>
</form>
</body>
</html>
No comments:
Post a Comment