I tried several things. i get invalid char or no object. can one change fore color or setup a mask. the text box contains numbers and if the number is negitive i need it to show in red, black if not.
var oRed = '#ff0000'
var oBlack = '#000000'
if (parseInt(document.getElementById('nm1TextBox').value) < parseInt(0))
{document.getElementById('nm1TextBox').forecolor = oRed;}
else
{document.getElementById('nm1TextBox').forecolor = oBlack;}

Dani AI

Generated

asked for negative numbers to appear in red; was right that the element's CSS color should be changed rather than a non‑existent forecolor property. A cleaner, more robust pattern is to let CSS handle presentation and have JavaScript decide the state (negative/positive/invalid) by toggling semantic classes. That avoids inline styling, handles decimals/commas, and makes maintenance easier.

/* keep colors in CSS */
input.negative { color: #c00; }
input.positive { color: #000; }
// attach an input handler that toggles classes
(function(){
  const el = document.querySelector('#nm1TextBox');
  if (!el) return;
  const update = () => {
    const raw = el.value.trim();
    const num = raw === '' ? NaN : Number(raw.replace(/,/g, ''));
    if (!isFinite(num)) {
      el.classList.remove('negative','positive');
      return;
    }
    el.classList.toggle('negative', num < 0);
    el.classList.toggle('positive', num >= 0);
  };
  el.addEventListener('input', update);
  update();
})();

Notes and troubleshooting: use Number() or parseFloat() for decimals (if parseInt() is used, pass a radix like parseInt(x,10)), strip thousands separators before parsing, and ensure the field exists before attaching handlers. Placeholder text and disabled inputs may be styled differently by the browser, so test those states. For accessibility, don't rely on color alone — keep the minus sign or add an explicit label/aria text for screen readers. If input masking or locale-aware formatting is needed, prefer an established small library (or input type="number" with careful validation) rather than ad‑hoc string hacks.

Recommended Answers

All 2 Replies

You need to access and modify the style collection. document.getElementById("nm1Textbox").style.color = "red";

thanks. i ended up changing the bgcolor. will try this in the next version.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.