0% found this document useful (0 votes)
6 views

Javascript_Validations (1)

The document provides a series of JavaScript programs demonstrating various functionalities, including expression evaluation, asterisk printing, email validation without regular expressions, and usage of predefined JavaScript objects. It also covers window objects for alerts and prompts, DOM manipulation for creating nodes, and form validation using regular expressions. Additionally, it includes a specific validation for contact numbers to ensure they start with 7, 8, or 9 and are 10 digits long.

Uploaded by

vijaya51good
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Javascript_Validations (1)

The document provides a series of JavaScript programs demonstrating various functionalities, including expression evaluation, asterisk printing, email validation without regular expressions, and usage of predefined JavaScript objects. It also covers window objects for alerts and prompts, DOM manipulation for creating nodes, and form validation using regular expressions. Additionally, it includes a specific validation for contact numbers to ensure they start with 7, 8, or 9 and are 10 digits long.

Uploaded by

vijaya51good
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Javascript Validations

1.Program that evaluates an expression

<html>
<head><title> evaluate </title>
<script type="text/javascript">
function result ( )
{
var a, b, c
a=document.fo.first.value;
c=eval(a);
document.fo.add.value=c
}
</script>
</head>
<body bgcolor=cyan >
<h1 align =center >Evaluation of an expression</h1>
<form name="fo"><br><br><br>
<h2 color=red>
Enter an expression</h2>
<input type="text" name="first"><br>
<br>
<h2 color=red>
Result</h2>
<input type="text" name="add">
<input type="button" value=evaluate onClick="result()">
<input type="reset" value="clear">
</form>
</body>
</html>
Output:

2. Program that prints a number of asterisks

<html>
<head><title> ASTERISK </title>
<script language="javascript">
function aster(){
var a,c="*",d=" ";
a=parseInt(document.form.text1.value)
for(b=1;b<=a;b++){
d=d+c; }
document.writeln(""+a+" is "+d+"");
}
</script>
</head>
<body text="black" name="stars" id="stars">
<h2 align="center"><font size="4" face="Verdana"> JAVA SCRIPT PROGRAM
<br>FOR PRINTING ASTERIKS
<form name="form" >
<input type=text name="text1" size="4">
<input type=button name=go value=go onclick=aster()>
</form>
</body>
</html>
3.Program that validates an e-mail ID without using Regular expression

<html>
<head>
<title> Email Validation </title>
<script type="text/javascript">
function val_email(field,alttxt)
{
with(field)
{
apos=value.indexOf("@");
dotpos=value.lastIndexOf(".");
if((apos<1) || (dotpos-apos<2))
{
alert(alttxt);
return false; }
else
{
alert("Valid e-mail address");
return true;
}
}
}
function val_form(thisform)
{
with(thisform)
{
if(val_email(email,"Not a Valid e-mail")==false)
{
email.focus();
return false;
}
}
}
</script>
</head>
<body >
<h1 align="center"><font face="Comic Sans MS" size="5"> EMAIL
VALIDATION </font></h1>
<form action="emailval.html" onsubmit="return val_form(this)" method="post">
E-Mail:
<input type="text" name="email" size="30"><br><br><br>
<input type="submit" value="SUBMIT" align="center">
</form>
</html>

Output:-
4.JavaScript predefined objects

<html>
<head>
<title> Predefined Objects</title>
</head>
<body >
<h2 align="center"><font size="4" face="Comic Sans MS"> DATE OBJECT
</font></h2>
<script type="text/javascript">
var d=new Date()
var weekday=new Array("sunday","monday","tuesday",
"wednesday","thursday","friday","saturday")
var month=new Array("Jan","Feb","Mar","Apr","May",
"Jun","Jul","Aug","Sep","Oct","Nov","Dec")
document.write(weekday[d.getDay()] + " ")
document.write(d.getDate() + ". ")
document.write(month[d.getMonth()] + " ")
document.write(d.getFullYear())
</script>
<br> <hr>
<h2 align="center"><font size="4" face="Comic Sans MS"> ARRAY OBJECT
</font></h2>
<script type="text/javascript">
var fname = new Array(6)
fname[0] = "INDIA"
fname[1] = "AUSTRALIA"
fname[2] = "AFRICA"
fname[3] = "USA"
fname[4] = "CHINA"
fname[5] = "JAPAN"

for (i=0; i<6; i++)


{
document.write(fname[i] + "<br>")
}
</script>
<br><hr>
<h2 align="center"><font size="4" face="Comic Sans MS"> MATH OBJECT
</font></h2>
<script type="text/javascript">
document.write(Math.round(11.75))
</script>
<br>
<script type="text/javascript">
document.write(Math.random())
</script>
<br>
<script type="text/javascript">
num=Math.random()*10
document.write(Math.round(num))
</script>
<br><hr>
</body>
</html>

Output:
5. using javascript window objects

<html>
<head>
<title> Java Script </title>
<script type="text/javascript">
function display_alert()
{
alert("U pressed an alert box")
}
</script>
<script type="text/javascript">
function disp_confirm()
{
var op=confirm("Select from below option")
if(op==true)
{
document.write("Welcome To Web Programming")
}
else
{
document.write("Enjoy Java Script")
}
}
</script>
<script type="text/javascript">
function disp_prompt()
{
var n=prompt("Enter some message"," ")
if(n!=null && n!=" ")
{
document.write("U have entered " +n)
}
}
</script>
</head>
<body bgcolor="purple">
<h2 align="center"><font size="4" face="Comic Sans MS"> WINDOW
OBJECTS </font></h2>
<form>
<input type="button" onclick="display_alert()" value="Alert Box">
</form>
<form>
<input type="button" onclick="disp_confirm()" value="Confirm Box">
</form>
<form>
<input type="button" onclick="disp_prompt()" value="Prompt Box">
</form>
</body>
</html>

Output:
HTML DOM

1.createnode

<html>
<body id="b1">
<script type="text/javascript">
// create a new paragraph
newpara = document.createElement("p");
// now some text
sometext = document.createTextNode("You are in wonderful CBIT world!!!\n");
newpara.appendChild(sometext);
// stick the paragraph onto an existing object
text = document.createTextNode("Enjoy the pleasure!!!");
newpara.appendChild(text);
obj1 = document.getElementById("b1");
obj1.appendChild(newpara);
document.write(obj1.nodeType+"<br/>");
document.write(obj1.nodeName+"<br/>");
document.write(newpara.innerHTML+"<br/>");
document.write(newpara.lastChild.nodeValue+"<br/>");
</script>
</body>
</html>

Output
2.API For accessing HTML content

<html>
<body bgcolor="red">
<p id="p1">javascript--- client side script</p>
<p>document object model----API for accessing html content</p>
<script type="text/javascript">
var pnode=document.getElementById("p1");
var ptext=pnode.innerHTML;
document.write(pnode.nodeType+"<br/>");
document.write(pnode.nodeName+"<br/>");
document.write(pnode.firstChild.nodeValue+"<br/>");
document.write(ptext);
</script>
</body>
</html>

Output

3. Retrieving elements by tag

<html>
<head><title>element by tag</title></head>
<body>
<p>tags demo</p>
<p>web programming</p>
<p>text here can be accessed using </p>
<script type="text/javascript">
var usetag=document.getElementsByTagName("p");
var tagtext=usetag[2].innerHTML;
document.write(tagtext);
</script>
</body>

Output

4.Form validation using RegExp Object

<html>
<head>
<title>Registration</title>

<script type="text/javascript">
function validation()
{
error="";
var re1=new RegExp("^[\\w]{6,}$");
var user=document.f.username.value;
if(!(re1.test(user)))
error+="username should contain 6 chars\n";
var re2=new RegExp("^[\\w]{6,}$");
var pwd=document.f.password.value;
if(!(re2.test(pwd)))
error+= "password should conatin atleast 6chars\n";
var cpwd=document.f.confirmpassword.value;
if(pwd.value!=cpwd.value)
error+="passwords do not match\n";
var re3=new RegExp("^[\\w]+@[\\w]+.co(m|.in)$");
var email=document.f.email.value;
if(!(re3.test(email)))
error+= "email should be in the form [email protected]\n";
var re4=new RegExp("^[\\d]{10}$");
var phone=document.f.phone.value;
if(!(re4.test(phone)))
error+= "phonenumber should cotain 10 digits";
if(error!="")
window.alert(error)
else
window.alert("Succesfull Registration");
return
}
</script>
</head>
<body bgcolor="dodgerblue">
<center>
<form name=f>
<table align=center>
<caption align=center> REGISTRATION </caption>
<tr >
<td>USERNAME :</td>
<td > <input type=text name=username></td>
<td> <p id= "uname"> </p></td>
</tr>
<tr>
<td>PASSWORD:</td>
<td ><input type=password name=password ></td>
<td> <p id= "pass"> </p></td>
</tr>
<tr>
<td>CONFIRM PASSWORD:</td>
<td ><input type=password name=confirmpassword ></td>
<td> <p id= "pass"> </p></td>
</tr>
<tr >
<td>E-mail ID:</td>
<td ><input type=text name=email ></td>
<td> <p id= "emai"> </p></td></tr>
<tr>
<td>CONTACT NO:</td>
<td ><input type=text name=phone ></td>
<td> <p id= "ph"> </p></td>
</tr>
<tr>
<td><input type=submit value="submit" onclick="validation()"></td>
<td><input type=reset value="reset"></td>
</tr>
</table>
</form>
</body>
</html>

Output

6.Validation for Contact number should begin with 7|8|9 and followed by 9 didgits(total length must be
10 digits

<html>
<head><script type="text/javascript">
function validate(){
var re =new RegExp ("^[7-9][0-9]{9}$");
var phonenumber=document.f1.p.value;
if(!re.test(phonenumber))
alert("enter a valid number");
}
</script>
</head>
<body>
<form name="f1" onsubmit="validate()">
contact number:<input type="text" name="p"/>
<input type="submit" value="submit"/>
</body>
</hrml>

You might also like