Find us on Google+ Kill the code: Palindrome validation in Javascript

Sunday 11 March 2012

Palindrome validation in Javascript

Preview :


New Web Project Enter a string :


Code :

<html>

    <head>

        <title>New Web Project</title>

    </head>

    <body>

     

     <script type="text/javascript">

      function pal() {

       var b;

       var a = document.getElementById('str');

       var c1 = a.value.length;

       var flag=0;

       for(b=0; b<c1/2;b++) {

        if(a.value.charAt(b)==a.value.charAt(--c1)) {

         flag=0;

        }

        else {

          flag=1;
          break;

        }

       }

       if(flag!=1) {

        document.getElementById("lbl").innerHTML = "The string is Palindrome";

        document.getElementById("fnt").style.color = "green";

       }

       else {

        document.getElementById("lbl").innerHTML = "The string is not Palindrome";

        document.getElementById("fnt").style.color = "red";

       }

       return;

      }

      

      function foc() {

       var a = document.getElementById("str");

       if(a.value == "enter string...") a.value = "";

       return;

      }

      function foc_lost() {

       var b = document.getElementById("str");

       if(b.value == "") b.value = "enter string...";

       return;

      }

     </script>

     

     

     

     

        Enter a string : <input type="text" name="string" id="str" value="enter string..." onfocus="return foc()" onblur="return foc_lost()"/>

        <input type="button" onclick="return pal()" value="Check"/><br />

        <font id="fnt"><label id="lbl"></label></font>

    </body>

</html>

1 comment: