Click to See Complete Forum and Search --> : form element validation problems


wbhk
10-23-2003, 12:00 PM
Hello,

I am trying to do a simple validation of a text form element that should only have numeric values. Below is the code I use to call a validation function from each form element, as well as for the functions themselves.
The problem lies with the focus() and select() functions in javascript. The functions correctly determine when an entered value is numeric or not, but do not subsequently select and focus on the correct, calling form element. Instead, the functions focus on the form element following the calling element. Any help greatly appreciated!

<input type="text" name="partic" size="15" onChange="checkIt(this)">

//***************************************************************************************************

//called by checkIt function; checks to see if passed string is a number or not
function isNumber(inputStr) {
for (var i = 0; i < inputStr.length; i++) {
var oneChar = inputStr.substring(i, i + 1)
if (oneChar < "0" || oneChar > "9") {
alert("You must enter a numeric value in this field.")
return false
}
}
return true
}

//***************************************************************************************************
//***************************************************************************************************
//***************************************************************************************************

//validates a text form element; calls isNumber function to see if passed value is a number
function checkIt(passed) {
inputStr = passed.value
if (isNumber(inputStr)) {
} else {
passed.focus();
passed.select();
}
}

requestcode
10-23-2003, 12:29 PM
I wonder if it is because you are returning false back to the function that it is nulifying the focus and selct. If you only want numbers and do not want commas or decimal points then you could use isNaN to check for numbers only like this:

function checkIt(passed) {
if(isNaN(passed.value))
{
alert("Must enter numbers only!")
passed.focus()
passed.select()
}
}

isNaN= is Not-a-Number

Charles
10-23-2003, 02:44 PM
<input type="text" onchange="if(isNaN (this.value)) {alert('Please enter a numeric value.'); this.value=''; this.focus()}">