/    Sign up×
Community /Pin to ProfileBookmark

Form validation script

Hi All,

I am by no means a JS expert and I am setting up form validation on my website. I got most of the validation elements working except for one particular field.

There is a text field in the form called “Group Size”. It can’t be a number field because sometimes people would enter text like “Approx 20 people”, or “40-50”. But I am getting some sort of spammy submissions on my form and they will usually put a group size of 1 or 2 or 5. And the email addresses are fake for sure. Anyway, I want to screen these out, so how do I do that? I’ve also appended the form code.

The conditions I want for the group size field are:
– If the group size field has a length of 1 AND
– The value of the field is less than 6
Then the script returns false and the alert pops up.

Thanks in advance!

Here is the JS and I have highlighted the part that I am stuck on.

function validation(form) {

if(form.first_name.value == ”) {

alert(‘Please enter your first name’);
form.first_name.focus();
return false;
}

if(form.last_name.value == ”) {

alert(‘Please enter your last name’);
form.last_name.focus();
return false;
}

if(form.company.value == ”) {

alert(‘Please enter your company or organization. *** We do not serve private groups such as birthday parties, wedding parties or other social events ***‘);
form.company.focus();
return false;
}

if(form.email.value == ”) {

alert(‘Please enter your email address’);
form.email.focus();
return false;
}

[B]
if (Number(form.00NG0000008iHKH.value) < 6 && form.00NG0000008iHKH.length == 1)

{
alert(‘Sorry but we are unable to provide programs for groups of 5 or fewer. If you still want to inquire, please phone us.’);
form.00NG0000008iHKH.focus();
return false;
}
[/B]

return true;
}

</script>

And here is the form:

<form action=”https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8” method=”POST” onsubmit=”return validation(this);”>

<input type=hidden name=”oid” value=”00DG0000000CS9I”>
<input type=hidden name=”retURL” value=”http://www.scavengerhuntanywhere.com/resources.php“>

<!– ———————————————————————- –>
<!– NOTE: These fields are optional debugging elements. Please uncomment –>
<!– these lines if you wish to test in debug mode. –>
<!– <input type=”hidden” name=”debug” value=1> –>
<!– <input type=”hidden” name=”debugEmail” –>
<!– value=”[email protected]“> –>
<!– ———————————————————————- –>

<label for=”first_name”>First Name</label><br><input id=”first_name” maxlength=”40″ name=”first_name” size=”30″ type=”text” /><br>

<label for=”last_name”>Last Name</label><br><input id=”last_name” maxlength=”80″ name=”last_name” size=”30″ type=”text” /><br>

<label for=”company”>Company</label><br><input id=”company” maxlength=”40″ name=”company” size=”30″ type=”text” /><br>

<label for=”email”>Email</label><br><input id=”email” maxlength=”80″ name=”email” size=”30″ type=”text” /><br>

<label for=”phone”>Phone</label><br><input id=”phone” maxlength=”40″ name=”phone” size=”30″ type=”text” /><br>

Program location<br><input id=”00NG0000008iHK7″ maxlength=”100″ name=”00NG0000008iHK7″ size=”30″ type=”text” /><br>

[B]Group Size<br><input id=”00NG0000008iHKH” maxlength=”20″ name=”00NG0000008iHKH” size=”30″ type=”text” /><br>[/B]

Event Date<br><input id=”00NG0000008iHKR” maxlength=”20″ name=”00NG0000008iHKR” size=”30″ type=”text” /><br>

Comments / Questions<br><textarea id=”00NG0000008iHKg” name=”00NG0000008iHKg” rows=”5″ cols=”50″ type=”text” wrap=”soft”></textarea><br>
<br>
<input type=”submit” name=”Continue ->”>

</form>

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@Logic_AliNov 11.2012 — I am getting some sort of spammy submissions on my form and they will usually put a group size of 1 or 2 or 5. And the email addresses are fake for sure. Anyway, I want to screen these out, so how do I do that?[/QUOTE]If you are getting bot submissions, then they won't use script so script can't stop them unless you use script to generate your form, or at least its 'action' parameter.

If you don't want your form to be script-dependent, you'll need to use server-side validation.
×

Success!

Help @andrew_long15 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 6.18,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...