JavaScript javascript validation for non empty,number and alphabets javascript check empty number

I have a form that accepts name, phone no. Currently, I have done validation for non empty fields. I want to add validation for name (alphabets) and phone no.(numbers).

My current code is given below. On submitting the form, I will be calling validateform() function:

function validateform()
{
    var str= true;
    document.getElementById("name").innerHTML="";

    if(document.frm.name.value=="")
    {
        document.getElementById("name").innerHTML="Please enter Name";
        str=false;
    }

    return str;
}

The value entered for name should be only alphabets. If not, I want to show the message "enter only alphabets". How should I do it?

Answer:1

/*mobile number of 10 digits */

 var digit=10; 

 var pattern=/[0-9]{digit}/;

   if(!pattern.match ( phoneno ) )
         alert("not a valid number");
Answer:2

As noted in the comments, you can try something like this:

var rule = /^[a-zA-Z]*$/;
if(rule.test(document.frm.name.value)) {
   // this contains only letters !
}
Answer:3

You could use regex to achive what you're trying to do.

    function validatePhonenumber(value) {
         var regexp = /^[0-9]+?$/;
         return regexp.test(value);
    }

    function validateAlphabet(value) {
         var regexp = /^[a-zA-Z ]*$/;
         return regexp.test(value);
    }
Answer:4
var alphaExp = /^[a-zA-Z]+$/;
if(!document.frm.name.match(alphaExp))
    {
        document.getElementById("name").innerHTML="Please enter only alphabets";
        str=false;
    }

var numExp = /^[0-9]+$/;
if(!document.frm.phone.match(numExp))
    {
        document.getElementById("phone").innerHTML="Please enter only numbers";
        str=false;
    }

With this you don't need to check for empty input. If you want to handle empty input separately, replace + with a * in the regexes.

Answer:5

I have the following JS code: var b=0; function Fetch_Comments() { var comment=document.getElementsByClassName("comnt_list"); for (i=0; i<comment.length; i++) { var ...

I have the following JS code: var b=0; function Fetch_Comments() { var comment=document.getElementsByClassName("comnt_list"); for (i=0; i<comment.length; i++) { var ...

The reply at MVC3 Pass Model view to controller using javascript implies that this was impossible, at least for MVC 3. I was wondering if there is any way in MVC 4 to quickly pass the entire form ...

The reply at MVC3 Pass Model view to controller using javascript implies that this was impossible, at least for MVC 3. I was wondering if there is any way in MVC 4 to quickly pass the entire form ...

I'm looking to move a div "#menu" to the side when it's clicked on. I'm using this code <script> $("#menu").click(function(){ $("this").animate({ marginLeft: "+=...

I'm looking to move a div "#menu" to the side when it's clicked on. I'm using this code <script> $("#menu").click(function(){ $("this").animate({ marginLeft: "+=...

  1. moving animations
  2. moving animations for powerpoint free download
  3. moving animations for powerpoint
  4. moving animation css
  5. moving animation images
  6. moving animation wallpaper
  7. moving animation pictures
  8. moving animations gif
  9. moving animation app
  10. moving animations for powerpoint free
  11. moving animation wallpaper for desktop
  12. moving animation background
  13. moving animation css codepen
  14. moving animation android
  15. moving animation in ppt
  16. moving animation sketchup
  17. moving animation maker
  18. moving animation photoshop
  19. moving animation software
  20. moving animations on instagram

I'm trying to create a simple meteor project based on the leaderboard example in their site. I want to add a small icon next to the score of each player. For doing so I created a folder named public ...

I'm trying to create a simple meteor project based on the leaderboard example in their site. I want to add a small icon next to the score of each player. For doing so I created a folder named public ...

  1. background image doesn't fill screen
  2. background image doesn't show css
  3. background image doesn't move when scrolling
  4. background image doesn't show
  5. background image doesn't work
  6. background image doesn't fill div
  7. background image doesn't show html
  8. background image doesn't work css
  9. background image doesn't display
  10. background image doesn't show blender
  11. background image doesn't scroll
  12. background image doesn't load
  13. background image doesn't show in safari
  14. background image doesn't show android
  15. background image doesn't fit
  16. background image doesn't show in firefox
  17. background image doesn't
  18. background-image url doesn't work
  19. css background image doesn't work
  20. html background image doesn't work