Click here to Skip to main content
15,895,777 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Requirement: textbox limit: 25characters, max limit:40char allow
IF text contains {State},count for {State} only is 16,
{City} count 14, {St}=2,{Zip}=5

Problem:
I have written JQuery code for this (below code) but it accepts
1.25chars={City}123456789—correct
2.25charcters=1234567891245678{City}-wrong- but it also accepting this one
3.25-chareacters=123456789123456789{City}---wrong- but it also accepting this one




1 dropdown:MainContent_ddlSelectAdLine Options: HeadLine

1 Textbox: MainContent_txtAdline onkeyup=”
<input name="ctl00$MainContent$txtAdline" type="text" id="MainContent_txtAdline" onkeyup="getBatch(event);" style="border-style:Inset;width:85%;" maxlength="50">

1 Span: MainContent_lblCount—for charcters Counting
50

Jquery Code:

JavaScript
function getBatch(e) {
            debugger;
            var len=0; 
            var cnt = $("#MainContent_txtAdline").val();
            var count = $("#MainContent_txtAdline").val().length;

            if (cnt.indexOf("{City}") > -1) 
   {len = 16;$("#MainContent_lblCount").html(16);count = count-6+len}
            else if (cnt.indexOf("{State}") > -1) { len = 14;  count = count - 7 + len }
            else if (cnt.indexOf("{St}") > -1) { len = 2; count = count - 4 + len }
            else if(cnt.indexOf("{Zip}")>-1){len=5; count=count-5+len}
            else { len = 0; count=count+len}     
           

            if (count <= 0) {
                count = 0;
            }
           
            var ddlslectedText = $("#MainContent_ddlSelectAdLine option:selected").text();
            if (ddlslectedText == "HeadLine") {
                if (cnt.indexOf("{City}") > -1)
 { $("#MainContent_txtAdline").attr('maxlength', '30'); }
                else if (cnt.indexOf("{State}") > -1) 
{ $("#MainContent_txtAdline").attr('maxlength', '30'); }
                else if (cnt.indexOf("{St}") > -1) 
{ $("#MainContent_txtAdline").attr('maxlength', '30'); }
                else if (cnt.indexOf("{Zip}") > -1) 
{ $("#MainContent_txtAdline").attr('maxlength', '40'); }
                else { $("#MainContent_txtAdline").attr('maxlength', '40'); }
               

                               
                if (count > 25) {
                    $("#MainContent_lblCount").html(count).css('color', '#CC0000');
                    
                }
                else { $("#MainContent_lblCount").html(count).css('color', '#000000'); }
            }
            
        }
Posted
Updated 24-Mar-15 2:12am
v2

1 solution

Input is not validated that it should start with {city} so it allows all the input .

Try using regexp validation.

if(/^{City}/.test(cnt)){
//do other validation
}

Reference: [reg exp object in java script]
 
Share this answer
 
v2

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