Test Input for Specific Value

by nickadeemus2002

HTML

<div>
<label>My Field </label>
<input type="text" id="my_field" value="|1||4||9||10|" />
</div>
    
<div>
    <label>Your Field </label>
<input type="text" id="your_field" value="|1||14||9||10|" />
</div>

CSS

div{margin:10px;padding:10px;}
input{padding:5px;}

JavaScript

//  I have an input field: $("#my_field")
//  Which contains the following following text: 
// |1||4||9||10|
// test for  |4|

function testInputForValue(inputValue){
    //if -1, |4| was not found
    var response;
    if(inputValue === -1){
        response = "|4| was not found in your input box";
    }else{
        response = "|4| was found in the input box at position " + inputValue;
    }
    
    alert(response);
}


$('input#my_field').click(function() {
   var inputValue = $(this).val();
   var isFound = inputValue.indexOf("|4|");
   testInputForValue(isFound);
});

$('input#your_field').click(function() {
   var inputValue = $(this).val();
   var isFound = inputValue.indexOf("|4|");
   testInputForValue(isFound);
});