dominoGuru.com

Your Development & Design Resource

Quick-Tip: Javascript Validation - String Comparisons

Just a quick one for today - really busy with several items!

I was recently running into an issue with my one of my HTML-based form's Javascript data validation. And I could have kicked myself when I found the issue. Here's an example of my form:

...
function checksub(val) {
 if (val == "99")
  { return false; }
}
....
<form onsubmit="return checksub(document.getElementById('test').value);" ...>
<input type="text" name="test" id="test" value=99 />
...

The above example would return True, of course, because the value of the test field is actually numeric 99 and not the string 99. To solve this, and any possible potential future occurances of such non-string datatypes making their way into my function, I modified it to the following:

...
function checksub(val) {
 val += "";
 if (val == "99")
  { return false; }
}
....
<form onsubmit="return checksub(document.getElementById('test').value);" ...>
<input type="text" name="test" id="test" value=99 />
...

This adds a blank string to the value of val, thus changing the datatype and allowing me to return False as I intended!


About the author: Chris Toohey

Thought Leadership, Web & Mobile Application Development, Solutions Integration, Technical Writing & Mentoring

A published developer and webmaster of dominoGuru.com, Chris Toohey specializes in platform application development, solutions integration, and evangelism of platform capabilities and best practices.



More from dominoGuru.com


dominoGuru.com is powered by IBM Notes Domino XPages & hosted by Prominic.NET

Contact Us

Use our Contact / Feedback form or one of these email addresses:

Creative Commons License

Except where otherwise noted, dominoGuru.com by Chris Toohey is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.