Raw Noobage
Date: 02/28/06
(Javascript Community) Keywords: html, asp, java
I'm very new to Javascript, and am trying to figure out how to do some form validation with an event handler. Don't worry, I'm doing this in my spare time, not for pay. =P
Whenever a user is finished with a field, I want it to be validated to make sure they haven't broken any rules. If an error comes up, I want to un-hide a hidden div and replace the div's contents with the error message(s). So far I've only entered one aspect of the validation to see if I can get the actual mechanisms of hiding/showing content working, but so far it's no good.
Here's my code:
function validateInput(id)
{
// get input referred to by id
var inputField = document.getElementById(id);
// define error messages array
var errors = new Array();
// get errors div
var errorsDiv = document.getElementById("javascript_errors");
if (id == 'string')
{
var valString = inputField.value;
if (valString.length == 0)
{
errors['string'] = 'No search string';
}
}
errorsDiv.value = errors.join('\n');
if (errorsDiv.value.length >= 1)
{
errorsDiv.style.display = 'block';
}
}
Which is called on the input
element via:
onblur="javascript:validateInput('string');
And then there's a div
I have in the HTML markup itself:
You should not see this message
I'd like to learn how to make this work (if possible) to expand my knowledge of how the language works, but if anyone has a better way of handling this I'd like to know that as well.
Source: http://community.livejournal.com/javascript/95377.html