944,125 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 10684
  • ASP.NET RSS
Oct 8th, 2009
0

JavaScript Validation on Textbox

Expand Post »
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.
Last edited by cVz; Oct 8th, 2009 at 5:02 am.
Similar Threads
cVz
Reputation Points: 29
Solved Threads: 7
Junior Poster
cVz is offline Offline
139 posts
since Mar 2008
Oct 8th, 2009
0
Re: JavaScript Validation on Textbox
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.
ASP.NET Syntax (Toggle Plain Text)
  1. <head>
  2. <script language="javascript" type="text/javascript">
  3.  
  4. function validateElements()
  5. {
  6. var username=document.getElementById('txtuserid').value;
  7.  
  8. if(username.length<12)
  9. {
  10. document.getElementById('lblError').innerText="Userid Less than 12 characters";
  11. return false;
  12. }
  13. }
  14. </script>
  15. </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
Reputation Points: 10
Solved Threads: 2
Newbie Poster
saradha is offline Offline
11 posts
since Jun 2009
Oct 13th, 2009
0

solution for u r problem

hi saradha add this code to regularexpression ' \w{6}\d{6}' and it will work if not send message
Reputation Points: 10
Solved Threads: 0
Newbie Poster
suryasasidhar is offline Offline
10 posts
since Sep 2009
Oct 13th, 2009
0
Re: JavaScript Validation on Textbox
Check if this helps:
ASP.NET Syntax (Toggle Plain Text)
  1. JS:
  2. <script type="text/javascript" language="javascript">
  3. function validate(sender,args) {
  4. args.IsValid = false;
  5. var tb = document.getElementById('txtInput');
  6. if (args.Value.match(/^\w{6}\d{6}$/)) {
  7. args.IsValid = true
  8. tb.style.backgroundColor = "white";
  9. tb.style.color="green";
  10. }
  11. else {
  12. tb.style.backgroundColor = "red";
  13. }
  14. }
  15. </script>
  16. Markup:
  17. <asp:TextBox ID="txtInput" runat="server"></asp:TextBox>
  18. <asp:Button ID="btnSubmit" runat="server" Text="Button" />
  19. <asp:CustomValidator ID="CustomValidator1"
  20. ControlToValidate="txtInput"
  21. runat="server" ErrorMessage="*"
  22. ClientValidationFunction="validate">
  23. </asp:CustomValidator>
Reputation Points: 12
Solved Threads: 8
Junior Poster in Training
guru_sarkar is offline Offline
68 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: Microsoft Server & What Programming IDE for developing ASP.Net Sites can I install?
Next Thread in ASP.NET Forum Timeline: Summary validation problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC