Check if input starts with certain character, output message and clear if not - Javascript jQuery

Javascript examples for jQuery:Form Checkbox

Description

Check if input starts with certain character, output message and clear if not

Demo Code

ResultView the demo in separate window


<html>
 <head> 
  <script src="https://code.jquery.com/jquery-1.11.0.min.js"></script> 
 </head> //from w w  w. ja  v a  2 s  .c o m
 <body> 
  <input type="text"> 
  <script>
var show="";
$('input').keyup(function(){
    var value = $(this).val();
    if (value.indexOf('+') != 0) {
      $(this).val('');
      if(show==""){
          console.log("Phone number must start with +");
      }
      show="1";
    }
});

      </script>  
 </body>
</html>

Related Tutorials