Click here to Skip to main content
15,912,069 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Dear All,

I have a textbox which needs to be validated as below:

1. It should accept only 12 characters.
2. First two letters should be 'BA'.
3. Next 10 letters should be integer(digit) values.

I need to validate it in the textchanged event.. Can any one plz help me on this?


Regards,
Raj
Posted

You could used following codes
For
1. It should accept only 12 characters.<br />

if ((form_name.text_name.value.length > 12))
{
   Alert("NAme shold less than 12 characters");
}

2. First two letters should be 'BA'
if( form_name.text_name.value.charAt(0)!='A'||form_name.text_name.value.charAt(1)!='B')
{
   Alert("invalide value");
}

and Yes Third is Your homework.
 
Share this answer
 
Comments
Raj.rcr 26-Apr-11 0:44am    
Thanx... My 5
[no name] 26-Apr-11 1:13am    
Its ok
1 ) Setting the Text box to take only 12 Values.

Why dont you limit the text for size modifying <input> tage used for the text box ?
<input type="text" size="value" maxlength="12"> </input>


2) First 2 should be BA.

You can use Substring operator and string comparison to achieve this.
Initialise actualString with value BA.

if ( stringSource.substring(0,1).toString() != actualString.toString() )
  {
      // Display Error
  }


3) Check the rest of the 10 digits are number.
if ( isNaN(stringSource.substring(2,11)))
  {
     // Display Erro
  }


You can add the above contents into one function and then set it to work for the textchanged event.

BR//
Harsha</input>
 
Share this answer
 
v4
Use regular expression to check. It's simple
here's a function you can use

function Check()
{
 var ex = /^BA[0-9]{10}$/;
 if(ex.test(document.getElementById('YourTextBoxId').value) == false)
 {
    // your code to warn the user of incorrect input
 }
}


You can call this function when the user leaves the textbox. Use the onblur event for it

<input type="text" onblur="Check();" />


Note that i have assumed that all 10 integers have to be entered. If you want it to be optional, replace the {10} above with {0,10}
 
Share this answer
 
Comments
ZeeroC00l 25-Apr-11 10:57am    
Good Answer buddy :)
Raj.rcr 26-Apr-11 0:42am    
Thank you... Its working fine..
C#
function validate()
   {
       var tb=document.getElementById ('TextBox1').value
       var f=tb.charAt(0)
       var s=tb.charAt(1)
       if(tb.value=="" )
       {
            alert('enter value')

       }
       if(f!='B')
       {
         alert ('First Char Should be B')
       }
       if(s!='A')
       {
          alert ('Second Char Should be A')
       }
      for(var i=3;i<13;i++)
      {
       if((tb.charAt(i) >= 48 && tb.charAt(i) <= 57) || tb.charAt(i) == 8 || (tb.charAt(i) >= 96 && tb.charAt(i) <= 105))
       {
                alert ('Enter Digits only')
        }

       }
       if(tb.length>13)
        {
          alert('Input Value Exceeds morethan 12')
          tb.value="";
         }



    return true

   }
</script>
 
Share this answer
 
Comments
Raj.rcr 26-Apr-11 0:45am    
My 5
I think u will not get this readymade validation code...!
You need to check that textbox value in a function

function CheckValue(src)
{
  if(src..length == 12)
  {
   if(src...2 = ='BA')
   {
      if(src....from 3 to 10 ==numbers)
        {
           return true;
        }
      else
        {
           return false;
        }    
   }
   else
   {
     return false;
   }
  }
  else
   {
     return false;
   }
}

I dont know the exact functions in javascript therefore i put ..... insted of function.
but i think this logic will work surely..!
All the Best !! Raj,
 
Share this answer
 
v2
Comments
[no name] 25-Apr-11 7:36am    
Added <pre> tag

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