/    Sign up×
Community /Pin to ProfileBookmark

form element validation problems

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();
}
}

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@requestcodeOct 23.2003 — 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
Copy linkTweet thisAlerts:
@CharlesOct 23.2003 — [font=monospace]<input type="text" onchange="if(isNaN (this.value)) {alert('Please enter a numeric value.'); this.value=''; this.focus()}">[/font]
×

Success!

Help @wbhk spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...