Monday, 16 August 2021

 Java Script

Function, Dialog Box, Validation and Regular Expression

Example:1

<html>   

   <head>

      <title>Form Validation</title>      

      <script type = "text/javascript">


        function validform()

        {

            var fname=document.f1.fname.value;

            var pass=document.f1.pass.value;


               if(fname==null||fname=="")

               {

                alert("name cant be blank");

                return false;


               }

               if(pass.length<6) 

               {

                    alert("password must be atleast 6 char long");

                    return false;


               }

               

             alert("both are correct");

             return true;


        }


    </script>

    </head>

<body>


   <form name="f1">


    Name:<input type="text" name="fname" size="20" placeholder="enter your full name"></br>

    Password:<input type="password" name="pass" size="20" placeholder="enter your password"></br>

    

    <input type="submit" name="submit" onclick="validform()">


   </form>

    

</body>

</html>



Example 2:


<!doctype html>

<html>

<head>

<title>Java Script-Function</title>

<script>


function swap()

{

var a,b,temp;

a=Number(document.getElementById("f").value);

b=Number(document.getElementById("s").value);


temp=a;

a=b;

b=temp;


document.getElementById("ans1").value= a;

document.getElementById("ans2").value= b;

}


</script>

</head>


<body>

Value of a: <input id="f">

Value of b: <input id="s"></br></br>


<button onclick="swap()">Swap</button></br></br>

Value of a: <input type="text" name="ans1" id="ans1">

Value of b: <input id="ans2" name="ans2" id="ans2">


</body>

</html>


Example 3:


<!doctype html>

<html>

<head>


<script>


function prod(price,taxrate,balance)

{

    

    var total=price+price*taxrate;


    if(total<=balance)

    {

        alert("Yes");


    }

    else

    {


        alert("No");


    }


}



</script>

</head>


<body>


   Price <input type="text" name="price">

   Balance <input type="text" name="balance">

    Tax Rate<input type="text" name="taxrate">


    <input type="button" value="Go" onclick="prod()">


</body>

</html>



Example:4


<html>   

   <head>

      <title>Form Validation</title>   

    <script>


      function showdttime()

      {

            document.getElementById("demo").innerHTML = Date();


           // document.myfrom.date1.value=Date();

      }



      function changebackground(val)

      {


           // var text1=""            ;


            

        if(val=='red')

        {

        document.body.style.background="red";

                   // document.body.style.color="red";

        }

        else if(val=='green')

        {

    document.body.style.background="green";

        }

              

      }



</script>

</head>


<body>

     

<p id="demo"></p>


<input type="button" onclick="showdttime()" value="finddatetime" />

<input type="button" id="rbtn" onclick="changebackground('red')" value="redbutton"/>

<input type="button" id="gbtn"onclick="changebackground('green')" value="greenbutton"/>

</body>

</html>


Example:5


<html>   

   <head>

      <title>Form Validation</title>      

      <script type = "text/javascript">


       var regex = /ca[kf]e/g;


   var str = "He was eating cake in the cafe.";

   

   var matches = str.match(regex);

   alert(matches.length);


    </script>

    </head>

<body>

</body>

</html>


Example 6:


<!DOCTYPE html>

<html>

<head>

<title>Search Word</title>

</head>

<body>

<script type="text/javascript">


var str = prompt("enter your string");

var srchword=prompt("enter word to search in string");

var copyword=0;


var temp = new Array();

temp = str.split(" ");

alert(temp.length);


for (var i=0;i<temp.length;++i)

{

 if(srchword.match(temp[i]))

 {

copyword=srchword.match(temp[i]);

var pos = temp.indexOf(srchword);

document.write(srchword+" found in your array at "+ pos +" position" );

copyword=-1;

 }

}

if(copyword==0)

{

document.write(srchword+" could not found in your array");

}

</script>


</body>

</html>



Example 7:


<html>  

<head>

  <script>  

  

    if(confirm("do you agree"))

    {

      alert("you have agreed");

    }

     else

     {

          name=prompt("Enter some string here");

          alert("Hi!!" +name);

          document.write("Your Name:"+name);

     }

  </script> 


</head>  

<body>  


</body>  

</html>   


Example:8

<html>  
<head>Factorial No.</head>  
<body>  
<script> 

  var i, no, fact;

fact=1;

no=prompt("Enter your value");

for(i=1; i<=no; i++)  
{
fact= fact*i;
}  

alert("Factorial No.is"+fact);
document.writeln(""+fact);
</script>  
</body>  
</html>   




Example:9


<html>  

<head>Sum of Number</head>  

<body>  

<script>  

  var num,i,sum=0;


  num=parseInt(prompt("Enter Sum of Number"));


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

    {

        sum=sum+i;

    }

        alert("Sum of Number" +sum);

        document.writeln(""+sum);

         

</script>  

</body>  

</html>   



Example:10


<html>  

<head>Popup Box</head>  

<body>  

<script>  

  

    var n;

    n=parseInt(prompt("Enter 1st number of addition"));


    if(confirm("second no.is 10"))

    {

        var sum=n+10;

        alert("first"+n+ "second" +sum);

        //document.write(+sum);

    }

</script>  

</body>  

</html>