/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] filter for numbers only

var card_number = eval(“document.checktest.card_number.value”);
var filter = /^[0-9]{2,4})+$/;
if (!filter.test(card_number))
{
alert(‘Your credit card number can only contain numbers and have no spaces.’);
return err(f.card_number.name);
}

Above is my code. I’m trying to check the user’s credit card number to make sure of two things. NO spaces, and all numbers and numbers only. I thought this was the right checker to develop, but it’s not working. Ignore the return err(f. etc. etc. because that is the name of the error checking function which I know works. If anyone can help I’d really appreciate it. Thanks!

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@ProggerPeteOct 24.2006 — var filter = /^d+$/;
Copy linkTweet thisAlerts:
@candycanuckOct 24.2006 — Here is the validator that I use. Unzip and use it as an include file. It will validate for numbers, length, blank space and much more.

I forget where I found this originally (I thought 4guys from Rolla, but I can't locate it on their site if it is there). The zip was provided as a download for free usage.

An example of how to use it
[CODE]
var ErrorMsg

function validatenow() {
var CanSubmit
ErrorMsg = "";
CanSubmit = true;
if( ForceNumber(myForm.cardNum, "Your credit card number can only contain the numbers 0-9.n") == false) CanSubmit = false
if (CanSubmit == false) alert(ErrorMsg);
return CanSubmit
}
[/CODE]
Copy linkTweet thisAlerts:
@ProggerPeteOct 24.2006 — What advantage does using what's in the zip have over just using the regular expression?
Copy linkTweet thisAlerts:
@candycanuckOct 24.2006 — The advantage of variety and convenience. Using the same code over again keeps things simple, especially if you need to do multiple validations within the same code.

Also, yours is really simple, but I don't understand it. I think it has been too long since I've seen something written in that format. And I always avoid using code that I can't understand without researching.
Copy linkTweet thisAlerts:
@ProggerPeteOct 24.2006 — True enough, blindly copying code you don't understand can be dangerous. Regular expressions are very worthy of learning though. I don't know how I ever did without them.
Copy linkTweet thisAlerts:
@bsstwebauthorOct 25.2006 — Thanks alot! That did the trick, and thanks for that extra include file, that has already been put to good use. Thanks very much!!
×

Success!

Help @bsstweb 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.20,
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,
)...