html - JavaScript - Do nothing if an input field is empty and if it has a value check to see if it is numeric and display an alert if it is not -


I am trying to create a form that automatically put the input field on blurring and a passive "total:" Displays the sum in the field. I do not want to run anything if a user focuses in an input, focus focus without inputting anything else, and if a user inputs then I only want to restrict the field to the field. If there is a better way of doing this, please let me know here is an example of my current approach:

html
   & Lt; Form method = "get" & gt; & Lt; Label = "january" & gt; January: & lt; / Label & gt; & Lt; Input type = "text" id = "jan" square = "zodiac" onblur = "isNum (); is calculated ();" & Gt; & Lt; Label = "February" & gt; For February: & lt; / Label & gt; & Lt; Input type = "text" id = "February" class = "zodiac" onblur = "isNum (); is calculated ();" & Gt; & Lt; Label = "total" & gt; Total: & lt; / Label & gt; & Lt; Input type = "text" id = "total" disabled & gt; & Lt; / Form & gt; & Lt; / Body & gt; & Lt; / Html & gt; Calculate the function   

javascript
  () {var elems = document.getElementsByClassName ('zodiac'); Var myLength = elems.length; Yoga = 0; For (var i = 0; i & lt; myLength; ++ i) {sum + = elems [i] .value * 1; } Document.getElementById ('Total'). Value = sum; } Function isNum () {var volume = purse (document.getElementsByClassName ('sum'). Value); If (INA (Quantity) == is true & amp; amp; volume! = '') {Warning ("Please enter a numeric value"); }}   

The calculation function currently works but "Please enter a numeric value" warning regardless of content every time I pop the tab from a field.

First you need to test the value of the element that loses focus, which means That you should give it in logic like this

  onblur = "isNum (this); calculation; sum ();"   

Then remove the javascript in your isNum function document.getElementsByClassName and instead use logic ... and check whether No amounts! = '' because it will never be equal to empty string when doing zodiac = purse (elem.value); You Elem.value

  function isNum (elem) {var Right = parseInt (elem.value); If (INNA (sum) == is correct & amp; amp; elem.value! = '') {Warning ("Please enter a numeric value"); }   

here

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -