Click here to Skip to main content
15,887,328 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
script to validate string contains 10 characters & 1 digit
Posted
Comments
LittleYellowBird 26-Aug-11 5:37am    
You need to ask a question. What is it that you need help with?
Srinivas Kumar Pasumarthi 26-Aug-11 5:48am    
i need accept string of 11 characters from users

that string should contain 10 alphabetics and 1 numeric character

JavaScript
function validate(id)
{
  num=id.value;
  if(num.length<11)
   {
    alert("please enter 11 characters");
    return false;
   }
  else
   {
var numcount=0;
var alphabetcount=0;
    for (n = 0; n < num.length; n++)
     {
      if( num.charCodeAt(n) >= 48 && num.charCodeAt(n) <= 57)
       {numcount=numcount+1; }
      else if((num.charCodeAt(n)>=65 && num.charCodeAt(n) <= 92) ||(num.charCodeAt(n)>=97 && num.charCodeAt(n) <= 122)) 
        {alphabetcount=alphabetcount+1;}
     }
   }
}
 
Share this answer
 
v5
Comments
hitech_s 26-Aug-11 5:51am    
based on the count you can raise alert
Herman<T>.Instance 26-Aug-11 6:01am    
and what if num.Length = 500 ?
hitech_s 26-Aug-11 6:07am    
good question..
i need to think about it ..
C#
<script language="javascript" type="text/javascript">
        function validate() {
            var chr = "poojadhonga";
            var i = 0;
            var chrcount=0;
            var deccount=0;
             var a;
             if( chr.length==11)
             {
            for (i = 0; i < chr.length ; i++)
            {
                 a = chr.charCodeAt(i);
                if(a >=65 && a <= 90 || a >= 97 && a <=122)
                {
                  chrcount++;
                }
                else
                {
                if( a >=48 && a<=57)
                {
                deccount++;
                }
                }
                }
                if( chrcount ==10 && deccount==1)
                {
                    alert("string is validate as  10 characters & 1 digit .");
                }
                else
                {
                alert(" string is not validate as 10 chracter and 1 integer");
                }


            }
            else
            {
             alert(" you have entered less charcter");
            }
        }
    </script>
 
Share this answer
 
JavaScript
if ( str.length != 11) 
  return false;

var l = str.match(/[A-Za-z]/g);
var p = str.match(/[0-9]/g);

if ( l.length != 10 || p.length != 1)
   return false;
return true;
 
Share this answer
 
Start here[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900