Numeric input field JavaScript custom validation2019 Community Moderator ElectionValidate decimal numbers in JavaScript - IsNumeric()How to validate an email address in JavaScript?How do JavaScript closures work?How to validate an email address using a regular expression?Which equals operator (== vs ===) should be used in JavaScript comparisons?How do I include a JavaScript file in another JavaScript file?How to replace all occurrences of a string in JavaScriptWhat does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?
Should a narrator ever describe things based on a characters view instead of fact?
UK Tourist Visa- Enquiry
Should I be concerned about student access to a test bank?
Did Nintendo change its mind about 68000 SNES?
Which partition to make active?
Do I need an EFI partition for each 18.04 ubuntu I have on my HD?
Why is this tree refusing to shed its dead leaves?
Is this Pascal's Matrix?
Emojional cryptic crossword
Can other pieces capture a threatening piece and prevent a checkmate?
What will the Frenchman say?
PTIJ: Why do we make a Lulav holder?
Why is indicated airspeed rather than ground speed used during the takeoff roll?
Exposing a company lying about themselves in a tightly knit industry: Is my career at risk on the long run?
Have any astronauts/cosmonauts died in space?
How are passwords stolen from companies if they only store hashes?
How to find the largest number(s) in a list of elements, possibly non-unique?
Pre-Employment Background Check With Consent For Future Checks
Turning a hard to access nut?
What are the rules for concealing thieves' tools (or items in general)?
Is VPN a layer 3 concept?
Why I don't get the wanted width of tcbox?
What is the difference between something being completely legal and being completely decriminalized?
What is the tangent at a sharp point on a curve?
Numeric input field JavaScript custom validation
2019 Community Moderator ElectionValidate decimal numbers in JavaScript - IsNumeric()How to validate an email address in JavaScript?How do JavaScript closures work?How to validate an email address using a regular expression?Which equals operator (== vs ===) should be used in JavaScript comparisons?How do I include a JavaScript file in another JavaScript file?How to replace all occurrences of a string in JavaScriptWhat does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?
I have an input numeric field where I registered all changed events:
- onchange
- onkeydown
- onkeyup
- onmousedown
- onmouseup
I need to permit inserting only numeric values with these rules:
- Values between 0 and 24
- It is possible to have decimal values.
- Decimal value must be have only 1 decimal digit.
- Decimal section must be a multiple of 0.5.
I need to immediately replace the old value if the current value is invalid.
So I have to accept, for example, values like these:
- 0
- 0.5
- 12
- 23.5
- 24
I can't accept values like these:
- Hello
- -0,
- -1
- .
- ,,
- .5
- 25
- --2
- ...
- e
I tried to use this code, but it does not cover all cases:
function customValidation(value) (parseFloat(value) <= 24 && parseFloat(value) >= 0));
function eventListner(el)
if (customValidation(el.value))
el.oldValue = el.value;
else if (el.hasOwnProperty("oldValue"))
el.value = el.oldValue;
javascript html regex validation
add a comment |
I have an input numeric field where I registered all changed events:
- onchange
- onkeydown
- onkeyup
- onmousedown
- onmouseup
I need to permit inserting only numeric values with these rules:
- Values between 0 and 24
- It is possible to have decimal values.
- Decimal value must be have only 1 decimal digit.
- Decimal section must be a multiple of 0.5.
I need to immediately replace the old value if the current value is invalid.
So I have to accept, for example, values like these:
- 0
- 0.5
- 12
- 23.5
- 24
I can't accept values like these:
- Hello
- -0,
- -1
- .
- ,,
- .5
- 25
- --2
- ...
- e
I tried to use this code, but it does not cover all cases:
function customValidation(value) (parseFloat(value) <= 24 && parseFloat(value) >= 0));
function eventListner(el)
if (customValidation(el.value))
el.oldValue = el.value;
else if (el.hasOwnProperty("oldValue"))
el.value = el.oldValue;
javascript html regex validation
So in your RegEx you need to match 1 or more digits followed by an optional decimal symbol and 5?
– PM 77-1
Mar 6 at 23:48
add a comment |
I have an input numeric field where I registered all changed events:
- onchange
- onkeydown
- onkeyup
- onmousedown
- onmouseup
I need to permit inserting only numeric values with these rules:
- Values between 0 and 24
- It is possible to have decimal values.
- Decimal value must be have only 1 decimal digit.
- Decimal section must be a multiple of 0.5.
I need to immediately replace the old value if the current value is invalid.
So I have to accept, for example, values like these:
- 0
- 0.5
- 12
- 23.5
- 24
I can't accept values like these:
- Hello
- -0,
- -1
- .
- ,,
- .5
- 25
- --2
- ...
- e
I tried to use this code, but it does not cover all cases:
function customValidation(value) (parseFloat(value) <= 24 && parseFloat(value) >= 0));
function eventListner(el)
if (customValidation(el.value))
el.oldValue = el.value;
else if (el.hasOwnProperty("oldValue"))
el.value = el.oldValue;
javascript html regex validation
I have an input numeric field where I registered all changed events:
- onchange
- onkeydown
- onkeyup
- onmousedown
- onmouseup
I need to permit inserting only numeric values with these rules:
- Values between 0 and 24
- It is possible to have decimal values.
- Decimal value must be have only 1 decimal digit.
- Decimal section must be a multiple of 0.5.
I need to immediately replace the old value if the current value is invalid.
So I have to accept, for example, values like these:
- 0
- 0.5
- 12
- 23.5
- 24
I can't accept values like these:
- Hello
- -0,
- -1
- .
- ,,
- .5
- 25
- --2
- ...
- e
I tried to use this code, but it does not cover all cases:
function customValidation(value) (parseFloat(value) <= 24 && parseFloat(value) >= 0));
function eventListner(el)
if (customValidation(el.value))
el.oldValue = el.value;
else if (el.hasOwnProperty("oldValue"))
el.value = el.oldValue;
javascript html regex validation
javascript html regex validation
edited Mar 7 at 2:41
karel
2,22352732
2,22352732
asked Mar 6 at 23:37
SafariSafari
4,0581462130
4,0581462130
So in your RegEx you need to match 1 or more digits followed by an optional decimal symbol and 5?
– PM 77-1
Mar 6 at 23:48
add a comment |
So in your RegEx you need to match 1 or more digits followed by an optional decimal symbol and 5?
– PM 77-1
Mar 6 at 23:48
So in your RegEx you need to match 1 or more digits followed by an optional decimal symbol and 5?
– PM 77-1
Mar 6 at 23:48
So in your RegEx you need to match 1 or more digits followed by an optional decimal symbol and 5?
– PM 77-1
Mar 6 at 23:48
add a comment |
2 Answers
2
active
oldest
votes
Sounds like you could benefit from using a "number" input type and just specifying the min, max, and step properties instead of using a RegEx for validity. For example:
<input type="number" name="myNumber" id="myNumber" min="0" max="24" step="0.5">
Then you can just check the validity with the standard methods available:
let myNumberInput = document.getElementById("myNumber");
let myNumberIsValid = myNumberInput.checkValidity();
The MDN web docs also has some useful information on their Constraint validation API page.
add a comment |
This regex covers all cases:
/^(?:(?:d|1d|2[0-3])(?:.[05])?|24(?:.0)?)$/
https://regex101.com/r/KZeVKa/1
Expanded:
^ # BOS
(?:
(?: # 0 - 23.5
d
| 1 d
| 2 [0-3]
)
(?: . [05] )?
| # or,
24 # 24.0
(?: . 0 )?
)
$ # EOS
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55033881%2fnumeric-input-field-javascript-custom-validation%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sounds like you could benefit from using a "number" input type and just specifying the min, max, and step properties instead of using a RegEx for validity. For example:
<input type="number" name="myNumber" id="myNumber" min="0" max="24" step="0.5">
Then you can just check the validity with the standard methods available:
let myNumberInput = document.getElementById("myNumber");
let myNumberIsValid = myNumberInput.checkValidity();
The MDN web docs also has some useful information on their Constraint validation API page.
add a comment |
Sounds like you could benefit from using a "number" input type and just specifying the min, max, and step properties instead of using a RegEx for validity. For example:
<input type="number" name="myNumber" id="myNumber" min="0" max="24" step="0.5">
Then you can just check the validity with the standard methods available:
let myNumberInput = document.getElementById("myNumber");
let myNumberIsValid = myNumberInput.checkValidity();
The MDN web docs also has some useful information on their Constraint validation API page.
add a comment |
Sounds like you could benefit from using a "number" input type and just specifying the min, max, and step properties instead of using a RegEx for validity. For example:
<input type="number" name="myNumber" id="myNumber" min="0" max="24" step="0.5">
Then you can just check the validity with the standard methods available:
let myNumberInput = document.getElementById("myNumber");
let myNumberIsValid = myNumberInput.checkValidity();
The MDN web docs also has some useful information on their Constraint validation API page.
Sounds like you could benefit from using a "number" input type and just specifying the min, max, and step properties instead of using a RegEx for validity. For example:
<input type="number" name="myNumber" id="myNumber" min="0" max="24" step="0.5">
Then you can just check the validity with the standard methods available:
let myNumberInput = document.getElementById("myNumber");
let myNumberIsValid = myNumberInput.checkValidity();
The MDN web docs also has some useful information on their Constraint validation API page.
answered Mar 7 at 0:01
musicinmyheadmusicinmyhead
1,27979
1,27979
add a comment |
add a comment |
This regex covers all cases:
/^(?:(?:d|1d|2[0-3])(?:.[05])?|24(?:.0)?)$/
https://regex101.com/r/KZeVKa/1
Expanded:
^ # BOS
(?:
(?: # 0 - 23.5
d
| 1 d
| 2 [0-3]
)
(?: . [05] )?
| # or,
24 # 24.0
(?: . 0 )?
)
$ # EOS
add a comment |
This regex covers all cases:
/^(?:(?:d|1d|2[0-3])(?:.[05])?|24(?:.0)?)$/
https://regex101.com/r/KZeVKa/1
Expanded:
^ # BOS
(?:
(?: # 0 - 23.5
d
| 1 d
| 2 [0-3]
)
(?: . [05] )?
| # or,
24 # 24.0
(?: . 0 )?
)
$ # EOS
add a comment |
This regex covers all cases:
/^(?:(?:d|1d|2[0-3])(?:.[05])?|24(?:.0)?)$/
https://regex101.com/r/KZeVKa/1
Expanded:
^ # BOS
(?:
(?: # 0 - 23.5
d
| 1 d
| 2 [0-3]
)
(?: . [05] )?
| # or,
24 # 24.0
(?: . 0 )?
)
$ # EOS
This regex covers all cases:
/^(?:(?:d|1d|2[0-3])(?:.[05])?|24(?:.0)?)$/
https://regex101.com/r/KZeVKa/1
Expanded:
^ # BOS
(?:
(?: # 0 - 23.5
d
| 1 d
| 2 [0-3]
)
(?: . [05] )?
| # or,
24 # 24.0
(?: . 0 )?
)
$ # EOS
answered Mar 7 at 0:52
slnsln
26.7k31638
26.7k31638
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55033881%2fnumeric-input-field-javascript-custom-validation%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
So in your RegEx you need to match 1 or more digits followed by an optional decimal symbol and 5?
– PM 77-1
Mar 6 at 23:48