Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want the user to enter session in textbox in the format yyyy-yy. I want masking for that. How to force and validate this?
Posted
Comments
Suvendu Shekhar Giri 24-Nov-15 1:57am    
Anything you have tried so far?
xpertzgurtej 24-Nov-15 2:01am    
tried maskededitextender but no luck
Snesh Prajapati 24-Nov-15 2:18am    
I hope it would be YYYY-MM ? .....if so please correct typo in question. Thanks.
xpertzgurtej 24-Nov-15 2:28am    
It is yyyy-yy. Bacause i want the user to enter academic session in this format e.g. 2014-15

If you did not find a masking control for your requirement, then just handle the keypress event for the textbox and check the length of the content. If content length is 4, then insert a "-", then allow two more characters.
 
Share this answer
 
[Edit] : keypress event will be more appropriate.
Try keypress event of jquery :


First allow only 7 characters in your text box by using maxlength property
HTML
<input type="text" id="txtInput"  maxlength="7" />


then write keypress
event in jquery :

JavaScript
$(document).ready(function () {
           $('#txtInput').keypress(function (e) { //write a keypressevent
               var inputVal = $(this).val();
               if (e.keyCode != 8) { //check if backspace is clicked or not
                   if (inputVal.length == 4) { //check the lenght of text input
                       inputVal = inputVal + '-'; //if length is 4 append '-'
                       $(this).val(inputVal);//then set the text box value by new appended text.
                   }
               }
           });
       });

Good luck.
 
Share this answer
 
v2
Comments
xpertzgurtej 24-Nov-15 7:35am    
Thanks. Its working with little modification to make it work on asp.net page..
Raje_ 24-Nov-15 7:36am    
Glad it helped. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900