Show data on any number input
Show data on any number input
I got this code that works for a pre-defined number (100200) but I would like to make it work for any random 6 digit number. The output remains the same.
I would like to show London, England as output in the given format when the user enters any 6 digit code from 000000 to 999999.
How do I got about this?
const $main = $('#zipcode');
const $inputs = $('.location');
const values =
trigger: '100200',
birds: ['London', 'England']
;
$main.on('keyup', function()
if (this.value === values.trigger)
$inputs.each(function(i, el)
el.value = values.birds[i];
);
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<input type="number" id="zipcode" class="form-control" required>
<hr />
<input type="text" id="location1" class="location" required>
<input type="text" id="location2" class="location" required>
values
trigger
birds
This is just for a demo with no backend or API. Any number between 000000 to 999999 to show the given output would be great.
– Elaine Byene
Aug 30 at 7:24
6 Answers
6
You just need to replace this.value === values.trigger
with this.value.toString().length === 6
this.value === values.trigger
this.value.toString().length === 6
This will check if the number of inputs are equal to 6, without taking into consideration what the input actually is.
const $main = $('#zipcode');
const $inputs = $('.location');
const values =
trigger: '100200',
birds: ['London', 'England']
;
$main.on('keyup', function()
if (this.value.toString().length === 6)
$inputs.each(function(i, el)
el.value = values.birds[i];
);
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<input type="number" id="zipcode" class="form-control" required>
<hr />
<input type="text" id="location1" class="location" required>
<input type="text" id="location2" class="location" required>
const $main = $('#zipcode');
const $inputs = $('.location');
const values =
trigger: '100200',
birds: ['London', 'England']
;
$main.on('keyup', function()
if (this.value.length >= 6 && this.value >=0 && this.value <= 999999)
$inputs.each(function(i, el)
el.value = values.birds[i];
);
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<input type="number" id="zipcode" class="form-control" required>
<hr />
<input type="text" id="location1" class="location" required>
<input type="text" id="location2" class="location" required>
u can change the condition to
if (this.value.toString().length === 6)
it just checks if the number has 6 digits.
sorry, but you where faster than me. ..
– Luis Curado
Aug 30 at 7:50
We can check length of input value after trimming it (just to avoid extra spaces) and validate if input is an integer value.
const $main = $('#zipcode');
const $inputs = $('.location');
const values =
trigger: '100200',
birds: ['London', 'England']
;
$main.on('keyup', function()
var thisValue = (this.value).trim();
if (thisValue.length == 6 && !isNaN(thisValue))
$inputs.each(function(i, el)
el.value = values.birds[i];
);
);
Try this in your if condition =
if (this.value >= 0 || this.value <=999999) {
Cool. However, I want to have a minimum criteria of 6 digits before the results are declared. Thank You.
– Elaine Byene
Aug 30 at 7:39
This is NOT what the OP wants. With this, if you write
0
you'll get the result, while he wants it to be minimum 000000
.– Islam Elshobokshy
Aug 30 at 7:39
0
000000
Try this code:
const $main = $('#zipcode');
const $inputs = $('.location');
const values =
trigger: '100200',
birds: ['London', 'England']
;
$main.on('keyup', function()
if (Number.isInteger(this.value))
if(this.value>=000000 && this.value<= 999999)
$inputs.each(function(i, el)
el.value = values.birds[i];
);
);
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You would need to get the
values
object from an API containingtrigger
andbirds
for all of the 6 digit numbers you'd like to have and then loop through them using the same way, no?– Islam Elshobokshy
Aug 30 at 7:23