/    Sign up×
Community /Pin to ProfileBookmark

Form validation help

Two questions:

[B]Question 1:[/B]
I currently have a drop down menu called “detail”. The script below is a simple script I use to make sure the user does not leave it blank. Directly below the script, I am including the select box. However, I need to prevent a user from selecting one of the values “Billing” between 12am and 9am. Please how can I disable just “billing” between these hours. All the other options can be selected at any time.

<script >
function checkDETAIL()
{
with(form) {

if (DETAIL.value == ”)
{
alert(“Please select a detail.”);
return false;
} else {
return true;
}
}

}
</script>
<select name=”DETAIL”>
<option>–Select One–</option>
<option value=”Billing”>Billing</option>
<option value=”Email”>Email</option>
<option value=”Browsing”>Browsing</option>
<option value=”Web Setup”>Web Setup</option>
</select>

[B]Question 2:[/B]
I have a textarea which is called “comments”, my script below prevents the user from leaving the textarea blank. Now I need to prevent the user from entering a credit card number inside the text area. I am completely clueless about this. My limit is the script I have right now and I am asking for any help.

Thanks to everyone.

<script>
function checkCOMMENTS()
{
with(form) {

if (COMMENTS.value == ”)
{
alert(“Please type in a description of the problem.”);
return false;
} else {
return true;
}
}

}
</script>

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@drlarkMay 23.2007 — As far as #1 is concerned, is the time for disabling relative to end user's browser or your server?

As far as #2 is concerned, you could make some rudimentary checks, but there is no way to absolutely guarentee that a user could put it a CC number. Now you could look for patterns like 1111-2222-3333-4444 or 1111222233334444 (the MC/Visa config), etc. If you are looking for a rudimentary check, then this can be done.

-dan
Copy linkTweet thisAlerts:
@FangMay 23.2007 — 1)For local time:window.onload=function() {
var now=new Date();
var hrs=now.getHours();
if(hrs&gt;9 &amp;&amp; hrs&lt;12) {
document.forms['formName']['DETAIL'].options[1]=null;
}
}
This removes the option. IE cannot disable options.
Copy linkTweet thisAlerts:
@sm0keyauthorMay 23.2007 — [B]Fang, [/B]

Thank you so much, that worked perfectly.

[B]drlark,[/B]

Thank you for your response, a rudimentary check is exactly what I need. So all I really need is to prevent an entry of 15 to 17 consecutive numbers. I dont know much JS so I couldn't even do that.

As always, any help would be greatly appreciated.
Copy linkTweet thisAlerts:
@drlarkMay 24.2007 — Here's a Credit Card check. I would suggest to place this function in an onChange() event for your <textarea>:

[CODE]<script>
function scan_card(field){
var cctype = new Array();
cctype["Visa"] = /4d{3}-?d{4}-?d{4}-?d{4}/g;
cctype["MasterCard"] = /5[1-5]d{2}-?d{4}-?d{4}-?d{4}/g;
cctype["Discover"] = /6011-?d{4}-?d{4}-?d{4}/g;
cctype["American Express"] = /3[4,7]d{13}/g;
cctype["Diners Club"] = /3[0,6,8]d{12}/g;

for(var cc in cctype){
if(cctype[cc].test(field.value)){
alert("There appears to be a "+cc+" number. Please remove and try again");
field.focus();
return false;
}
}
return true;
}
</script>
[/CODE]

For the textarea, do something like this:[code=html]
<textarea name="comments" onChange="scan_card(this);"></textarea>[/code]

This code was adapted from this page. Please notice a couple of things, I added a 'g' to the end of the regex to sacn the who value. I also put the check into an array to allow for a variety of detections (Visa, then MC, then ...). It could be generalized to work from an onSubmit(). From personal experience, I generally tend to like to validation as the user goes through the form.

Let me know how this works for you.

-dan
Copy linkTweet thisAlerts:
@sm0keyauthorMay 24.2007 — drlark,

You are the BEST!!!

Thank you.

Thank you Dan and Thank you Fang.
×

Success!

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