简体   繁体   中英

how to validate cell number

i have to validate the cell no and my requirements are :

1.filed must not empty 2. if user enter alphabetic value it pop-up "alphabets are not allowed" 3. field must start with "+" sign 4. if filed value is less than 13 it pop-up "please enter valid phone no"

i am using this code..

function validateForm()
{

    var cell = document.reg_form.cellno.value;
            if(cell.length==0) 
            {
                alert("Please enter cell number");
                reg_form.cellno.focus();
                return false;
            }

           if(isNaN(cell)||cell.indexOf(" ")!=-1)
              {
                 alert("Enter numeric value")
                 return false; 
              }


            if (cell.charAt(0)!="+")
              {
                alert("Cell no should start with +");
                return false
              }

                if(cell.length < 13) 
               {
              alert("You have entered wrong number");
              reg_form.cellno.focus();
              return false;
        }
return true;
}

some code is not working here when i enter numeric value.. it shows {"Cell no should start with "+"} when i put {+} sign it says please enter numeric value when i enter only single numeric value like {9} it goes forward.. although in this way field has only 2 value "+" and "9".. it should pop-up {"You have entered wrong number"}

please tell me where i made the mistake....

A regular expression that matches only a plus sign and 12 digits:

function validateForm(){
    var cell = document.reg_form.cellno;
     return  /^\+\d{12}$/.test(cell.value);
}

如果值大于13,则将单元格长度与13进行比较将返回true(并发出警报)。我怀疑您想要

if(cell.length < 13) 
function validateForm()
{
     var cell=document.reg_form.cellno.value;
     var msg="";
     if(cell.length==0) 
     {
         msg="Please enter cell number";
         alert(msg);
         reg_form.cellno.focus();
         return false;
     }

     if(isNaN(cell)) msg+="\nEnter numeric value";
     if (cell.charAt(0)!="+") msg+="\nCell no should start with +";
     if(cell.length != 13) msg+="\nCell number must be within 13 characters";
     if(msg) 
     {
         alert((msg));
         reg_form.cellno.focus();
         return false;
     }
     return true;  
}

An example is here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM