0

how to check if the text exists in text box jquery

I need to check if the text exists in the input box,if checking text is not exist thend append new data

I have tried with below code

    $(document).ready(function(){
       $("#item_inv_desc").change(function(){
            var item_inv_desc = $('select[name=item_inv_desc]').val();
            if (item_inv_desc == 7) 
            {

                var invoice_number = "123456789";
                //I need check if text "CRN" exists in text box
                var data=$('#invoice_number:contains("CRN")')
                if(data)
                {
                     //if text "CRN" exist no need to append data 
                }
                else
                {
                     //if not exist
                     $("#invoice_number").val(invoice_number+"CRN");
                }
            }
       }); 
    })

    //Html
    <input type="text" id="invoice_number"  value="">

I am having problem when try to insert append value it adds extra CRN number to invoice number,I need to avoid duplicating,

3
  • Try show us the corresponding HTML code, second off, look into indexOf() Commented May 3, 2018 at 7:08
  • Edit this var data=$('#invoice_numbe:contains("CRN")') to var data=$('#invoice_number:contains("CRN")') Commented May 3, 2018 at 7:09
  • Try with "if(data.length > 0)". Commented May 3, 2018 at 7:10

3 Answers 3

1

Try includes() with the val() of the text box:

var data = $('#invoice_number').val().includes("CRN")

or, for older browsers, use indexOf():

var data = $('#invoice_number').val().indexOf("CRN") !== -1

Working Demo:

$(document).ready(function() {
  $("#invoice_number").change(function() {
    var invoice_number = "123456789";
    var data = $('#invoice_number').val().includes("CRN");

    if (data) {
    } else {
      $("#invoice_number").val(invoice_number + "CRN");
    }
  });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="invoice_number" value="">

Sign up to request clarification or add additional context in comments.

3 Comments

Sir, actually I have to find text "CRN" from the number like 123456789CRN, Input box value will be like 123456789CRN
@ManjunathCa you should be using replace check my answer
@ManjunathCa -- see my updated answer. Try adding some value to the input which contains/doesn't contain CRN and observe the behaviour.
0

You can use regex to remove all the digits and get the text only from the value of the input text. If you want that on button click then add that block of code inside the click function:

var data = $('#invoice_number').val();
var res = data.replace(/[0-9]/g, '');
console.log(res);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="invoice_number"  value="123456789CRN">

Comments

0

@Manjunath var data=$('#invoice_numbe:contains("CRN")')

"r" is missing when you are checking below var data=$('#invoice_numbe:contains("CRN")')

and use

var data=$('#invoice_number').val().indexOf("CRN");
        if(data>-1){
            // donot add CRN
        }

1 Comment

@ManjunathCa add ; at the last to close the statement

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.