0

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.

Edited by cVz: n/a

4
Contributors
3
Replies
15
Views
8 Years
Discussion Span
Last Post by guru_sarkar
0

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

0

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>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.