JavaScript Validation on Textbox

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 136
Reputation: cVz is an unknown quantity at this point 
Solved Threads: 7
cVz's Avatar
cVz cVz is offline Offline
Junior Poster

JavaScript Validation on Textbox

 
0
  #1
Oct 8th, 2009
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.
Delphi & C# programmer deluxe...
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 11
Reputation: saradha is an unknown quantity at this point 
Solved Threads: 2
saradha's Avatar
saradha saradha is offline Offline
Newbie Poster
 
0
  #2
Oct 8th, 2009
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.
  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
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 10
Reputation: suryasasidhar is an unknown quantity at this point 
Solved Threads: 0
suryasasidhar's Avatar
suryasasidhar suryasasidhar is offline Offline
Newbie Poster

solution for u r problem

 
0
  #3
Oct 13th, 2009
hi saradha add this code to regularexpression ' \w{6}\d{6}' and it will work if not send message
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 40
Reputation: guru_sarkar is an unknown quantity at this point 
Solved Threads: 6
guru_sarkar guru_sarkar is offline Offline
Light Poster
 
0
  #4
Oct 13th, 2009
Check if this helps:
  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>
Reply With Quote Quick reply to this message  
Reply

Tags
asp, c#, javascript, textbox, validation

Message:



Similar Threads
Other Threads in the ASP.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC