How to prevent your javaScript code from syntax errors

The Syntax error in the JavaScript code is very common error type when your write the code. Generally when you write the wrong structure of the JavaScript code , browser will give the syntax error at the time of interpretation. Small syntax error in the JavaScript code on a Web page can stop the script from working correctly. Continue reading “How to prevent your javaScript code from syntax errors”

Example: test() and exec() methods of the RegExp in JavaScipt

We know that regular expression is just string pattern or simple string that describes the sequence of characters for searching something in the specified string. The test() method of any regular expression object returns the Boolean value that indicates whether or not the searched pattern exists in specified string. Continue reading “Example: test() and exec() methods of the RegExp in JavaScipt”

Validate URL starts with http: in javascript

 
Regular expression for validating url that starts with http: in JavaScript such as https://www.authorcode.com.

   <html>  
      <head>  
          <script type="text/javascript">  
          var reg = /^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&amp;%\$#_]*)?$/;  
          function URLValidation(url)
          {  
            var OK = reg.exec(url.value);  
            if (!OK)  
              window.alert("URL isn't  valid");  
            else  
              window.alert("URL is  valid");  
          }  
        </script>  
      </head>  
 
      <body>  
        <p>Enter your url address then press Enter.</p>  
        <form action="">  
          <input name="txturl" onchange="URLValidation(this);">  
        </form>  
      </body>  
    </html>

Regular expression to check the currency upto 2 digit in JavaScript

 
Regular expression to check the currency upto 2 digit in JavaScript such as 34, 45.45, 567.45 etc.

    <html>  
      <head>  
          <script type="text/javascript">  
          var reg = /^\d+(\.\d\d)?$/;  
          function CurrencyValidation(Currency)
          {  
            var OK = reg.exec(Currency.value);  
            if (!OK)  
              window.alert("Currency isn't  valid");  
            else  
              window.alert("Currency is  valid");  
          }  
        </script>  
      </head>  
 
      <body>  
        <p>Enter your Currency up to two decimal numbers and then press Enter.</p>  
        <form action="">  
          <input name="txtCurrency" onchange="CurrencyValidation(this);">  
        </form>  
      </body>  
    </html>

Regular expression for password validation in JavaScript

 
Following example show that how we can validate password with the help of Regular Expression that have these validation:

Must be at least 10 characters
Must contain at least one one lower case letter,
one upper case letter,
one digit and one special character
Valid special characters are – @#$%^&+=

  <html>  
      <head>  
          <script type="text/javascript">  
          var reg = /^.*(?=.{10,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$/;  
          function PhoneValidation(phoneNumber)
          {  
            var OK = reg.exec(phoneNumber.value);  
            if (!OK)  
              window.alert("phone number isn't  valid");  
            else  
              window.alert("phone number is  valid");  
          }  
        </script>  
      </head>  
 
      <body>  
        <p>Enter your phone number and then press Enter.</p>  
        <form action="">  
          <input name="txtPhone" onchange="PhoneValidation(this);">  
        </form>  
      </body>  
    </html>

More on Regular Expressions