1

I'm can't figure out a way of displaying a message if a specific word is inputed into an input box. I'm basically trying to get javascript to display a message if a date, such as '01/07/2013', is inputed into the input box.

Here is my html <p>Arrival Date</p> <input type="text" id="datepicker" id="food" name="arrival_date" >

I'm using a query data picker to select the date.

0

3 Answers 3

1

You can insert code in attribute onchange

onchange="if(this.value == 'someValue') alert('...');"

Or create new function

function change(element){
    if(element.value == 'someValue'){
        alert('...');
    }
}

And add attribute

onchange="change(this);"

Or add event

var el = document.getElementById('input-id');
el.onchange = function(){
    change(el); // if 'el' doesn't work, use 'this' instead
}

I'm not sure if it works, but it should :)

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

Comments

1

Use .val() to get the value of the input and compare it with a string

var str = $('#datapicker').val(),   // jQuery

   // str = document.getDocumentByI('datapicker').value ( vanilla js)

    strToCompare = '01/07/2013';

if( str === strToCompare) {

    // do something
}

And encase this in either change or any keyup event to invoke it..

$('#datepicker').change(function() {

     // code goes here
});

Update

Try the code below.

$(function () {
    var $datepicker = $('#datepicker');
    $datepicker.datepicker();
    $datepicker.on('change', function () {
        var str = $datepicker.val(),
            strToCompare = '07/19/2013';

        if (str === strToCompare) {

            console.log('Strings match')
        }
        else {
             console.log('boom !!')   
        }
    });
});

Check Fiddle

6 Comments

Sorry man, it didn't work. I added document.write within the (do something) section and it didn't display any text.
can you show the code that you have written.. Avoid using document.write .. Try using console.log
allardadam.me.uk/javascript -- I just uploaded
You have not specified the change evnet handler
I just changed the javascript method
|
0

Your input has 2 ids. You need to remove id="food". Then the following should work with IE >= 9:

document.getElementById('datepicker').addEventListener(
  'input',
  function(event) {
    if (event.target.value.match(/^\d+\/\d+\/\d+$/))
      console.log("Hello");
  }, false);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.