Hi

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

LLLLLLNNNNNN

Where

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.

Regards.

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.

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

 if(username.length<12)
 {
 document.getElementById('lblError').innerText="Userid Less than 12 characters";
 return false;
}
}
</script>
</head>

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.

cheers,
saradha

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

Check if this helps:

JS:
<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";
                tb.style.color="green";
            }
            else {
                tb.style.backgroundColor = "red";
            } 
      }
    </script>
Markup:
<asp:TextBox ID="txtInput" runat="server"></asp:TextBox>       
        <asp:Button ID="btnSubmit" runat="server" Text="Button" />
        <asp:CustomValidator ID="CustomValidator1" 
        ControlToValidate="txtInput"
        runat="server" ErrorMessage="*"
        ClientValidationFunction="validate">
        </asp:CustomValidator>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.