I use Visual 08 ASP.NET C# and JavaScript

I would like to do the following validation on a text box

the length MUST be 12 characters long in the following format



L = Letters
N = Numbers

if the users input does not match the required format i want the textbox's backcolor to go red , but if the users input matches the required format i want the textbox's text to go green.

I have absolutely no idea how to do this in Javascript so any help would be appreciated.

Thanks in advance.


I have a solution for the first problem for you . ie., checking out if length is 12 characters long.
Say if u have a txt box labeled "txtuserid" and a label to display error "lblError"

u can use the following function in java script to check the length and display error if the length is less than 12.

<script language="javascript" type="text/javascript">
function validateElements()
 var username=document.getElementById('txtuserid').value;

 document.getElementById('lblError').innerText="Userid Less than 12 characters";
 return false;

for the next requirement of yours ie., for checking if it is 6 letters followed by 6 numbers, i suggest that u can use the regular expression validator control and get the text box validated.


hi saradha add this code to regularexpression ' \w{6}\d{6}' and it will work if not send message

Check if this helps:

<script type="text/javascript" language="javascript">
        function validate(sender,args) {
            args.IsValid = false;
            var tb = document.getElementById('txtInput');
            if (args.Value.match(/^\w{6}\d{6}$/)) {
                args.IsValid = true
                tb.style.backgroundColor = "white";
            else {
                tb.style.backgroundColor = "red";
<asp:TextBox ID="txtInput" runat="server"></asp:TextBox>       
        <asp:Button ID="btnSubmit" runat="server" Text="Button" />
        <asp:CustomValidator ID="CustomValidator1" 
        runat="server" ErrorMessage="*"