Reputation: 299
I using javascript to check the length of height input by the user and but the problem I am facing is that even javascript just check the first number of the input and throws an error. Just for eexample if I have to input 45 in the height, I get an error height must be between 6-36 just after entering 4 it doesnt let me enter 5 and when i try to remove move and it input becomes empty It again throws an error height must be between 6-36. Please help me find the problem.
<input type="number" id="message1" name="height" oninput="function_two()">
function function_two() {
var FrameHeight = document.getElementsByName('height')[0].value;
if (FrameHeight <= 36 && FrameHeight >= 6)
return true;
else
alert("Height must be between 6-36");
}
Upvotes: 0
Views: 6341
Reputation: 13
I would think focus out event would help https://api.jquery.com/focusout/
Upvotes: 0
Reputation: 65806
You are using the wrong event (input
), which fires as any input is given to the field. Use the change
event, which fires when the value changes and the field loses the focus.
Additionally, separate your JavaScript from your HTML. Do your event handling with modern, standards-based practices, rather than with inline HTML event attributes, which should not be used.
See the comments below for other adjustments to the solution that make the code more efficient and/or update it to modern standards.
// Get your DOM reference just once. .querySelector() is preferred
// over .getElementsByName, .getElementsByTagName, .getElementsByClassName
// as the former returns a static node list and the latter(s) return
// live node lists that hurt performance.
let nameInput = document.querySelector("input[name='height']");
// And set up event handlers in JavaScript, not HTML
nameInput.addEventListener("change", rangeCheck);
// Name functions with descriptive names as to what they do.
// Don't use the word "function" in a function name.
function rangeCheck() {
// In a DOM event handler, you can just use "this" as a reference
// to the DOM element that triggered the event.
var FrameHeight = this.value;
// Just test for the bad values and act accordingly
if (FrameHeight < 6 || FrameHeight > 36) {
alert("Height must be between 6-36");
}
}
<input type="number" id="message1" name="height">
Upvotes: 1
Reputation: 620
The function is ok. 4 is less than 6 therefore it throws an error. The best way forward is to run function after user has finished typing. To do this, edit the html section to onchange.
<input type="number" id="message1" name="height" onchange="function_two()">
function function_two() {
var FrameHeight = this.value;
if( trim (value ) == '' ){
return false;
}
if (FrameHeight < 6 || FrameHeight > 36) {
alert("Height must be between 6-36");
}
}
Upvotes: 1
Reputation: 4104
How is the code supposed to know how many digits you want to add? So that it triggers the validation? At the moment, the function is called every time the user gives an input, that is every time a user types.
I suggest you either use change
as the event, that is fired when user leaves the input after it changes it, or change the type to range
. This way you don't need to do the validation.
I would advice you use <input type="range" min="6" max="36" step="1" />
if you can. Note that it is not supported by all browsers as it is a HTML5 element.
Upvotes: 2
Reputation: 535
You are validating each keystroke entered, so you may want to combine a combination of oninput and onblur to validate the entire value and the keystrokes as they happen.
Try something like this:
<html>
<head>
<style>
input.invalid {
background-color: red;
}
</style>
<script>
function validateInput(inputElement) {
var frameHeight = inputElement.value
if (isValidHeight(frameHeight)) {
inputElement.className = ""
} else {
inputElement.className = "invalid"
}
}
function validateFrameHeight(inputElement) {
var frameHeight = inputElement.value
if(!isValidHeight(frameHeight)) {
alert("Height must be between 6-36");
}
}
function isValidHeight(frameHeight) {
return frameHeight <= 36 && frameHeight >= 6
}
</script>
</head>
<body>
<input class="invalid" type="number" id="message1" name="height" oninput="validateInput(this)" onblur="validateFrameHeight(this)">
</body>
Upvotes: 0
Reputation: 1603
Sounds like you have a few problems and therefore a couple possible solutions
If you want the error to appear after your first character but still want to be able to keep entering characters until the requirements are met, try displaying the error by adding it to your page somewhere instead of using alert.
If you want to only check the value when someone is done typing, you can use onchange instead of oninput, though this means that the user will have to defocus the input.
If you want to check the value when someone is done typing but without having to defocus the input you should look into using a debounce function. Underscorejs has a good one or you can write your own.
Upvotes: 1