/    Sign up×
Community /Pin to ProfileBookmark

Form Validation

Hi there!
i am trying to validate my form for the following before it submites to a db. but i am having lots of trouble. so i am back to exprets help
1. it should check only for Numbers in a field
2. it should check for a .[period] in a field
can somebody help me please
Thanks
krs

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@graatzJul 01.2005 — well, I have a VBS function that works just fine but I'm having problems with a JS one for no good reason...

VBS code (works):
[CODE]function isNumberPeriod(strInput)
strValid = "0123456789"
dim hasPeriod, strTest
for i = 1 to len(strInput)
strTest = mid(strInput,i,1)
if instr(strValid, strTest) = 0 then
if strTest = "." then
hasPeriod = hasPeriod + 1
else
isNumberPeriod = false
exit function
end if
end if
next
if hasPeriod <> 1 then
isNumberPeriod = false
exit function
end if
isNumberPeriod = true
end function[/CODE]


So in your (JS) validation function, you can use a line like:

If isNumberPeriod(document.form_name.input_name.value) {

...

Of course, since the function is VBS it won't work unless someone is using IE. Below I have the JS version that [B]doesn't work[/B] but is meant to follow the same logic as the above function

JS code ([I]doesn't work[/I])
[CODE]function isNumberPeriod(strInput) {
var strValid = "0123456789";
var hasPeriod = 0;
var strTest;
for (var i = 0; i < strInput.length; i++) {
strTest = strInput.charAt(i);
if (strValid.indexOf(strTest)) == -1) {
if (strTest == ".") {
hasPeriod++;
}
else {
return false;
}
}
}
if (hasPeriod != 1) {
return false;
}
return true;
}[/CODE]


Can anyone fix the above code? It's probably something dumb.. I'm apt to make stupid mistakes that I can't catch cuz I code in notepad :p
Copy linkTweet thisAlerts:
@crh3675Jul 01.2005 — isNaN is a built-in function for numerics

<i>
</i>

function numHasPeriod(val){
if(isNaN(val)){
return false;
}
if(!String(val).match(/./)){
return false;
} <br/>
return true;
}


Copy linkTweet thisAlerts:
@BigMoosieJul 01.2005 — String.prototype.isNumber=function(){
return this*1==this;
}

String.prototype.lessThanTwoDecimals=function(){
return this.indexOf(".")==this.lastIndexOf(".")
}
×

Success!

Help @krs4wd 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.17,
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,
)...