/    Sign up×
Community /Pin to ProfileBookmark

Defining validation rules

I am very new to this. Trying to add a javascript to a simple form. The script is supposed to validate the information on the user’s side. I found the script at javascript.internet.com. It references a web site [url]http://www.hagedesign.dk/scripts/js/validation[/url] which is no longer valid (and not in English). I need to know what it means “Define the validation rules for each field.” the code says: define(‘field1’, ‘string’, ‘Apple’); My form has a few fields, name, address, zip, etc. Any help given will be deeply appreciated. Cmb.

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@Warren86Feb 23.2005 — Below is a very simplistic form "validation." Defining the rules, means to compare the current value of a field or other form element, with a valid value. For a more specific example, post your code, including any JS you already have, and the HTML form.

<HTML>

<Head>

<Script Language=JavaScript>

function validate(isForm){

ampersand = isForm.email.value.match("@");
isValid = true;
if (isForm.user_name.value.length>10)
{
alert("Your name must be less than 10 characters");isValid = false}
if (ampersand == null)
{
alert("Not a valid e-mail!");isValid = false;
}
if (isForm.password.value.length <1 || isForm.password.value.length >5)
{
alert("The password must be between 1 and 5 characters");isValid = false;
}
if (isValid){myForm.submit()}
}


</Script>

</Head>

<Body>

<Form name='myForm'>

User Name <input text name='user_name' size=6><br>

Email <input text name='email' size=6><br>

Password <input text name='password' size=6><br>

<input type=button value='Submit' onclick="validate(myForm)">

</Form>

</Body>

</HTML>
Copy linkTweet thisAlerts:
@cmbauthorFeb 23.2005 — <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<script language="JavaScript" src="validation.js"></script>

<meta http-equiv="content-type"

content="text/html; charset=iso-8859-1" />

<title>Ordering Trees</title>

</head>

<body>

<h2>Ordering Your Tree Lawn Tree</h2>

<form method="post" action="http://www.cookwood.com/cgi-bin/display_results.pl">

Name: <input type="text" name="name" />

Address: <input type="text" name="address" size="30" />

<p>City: <input type="text" name="city" />

State: <input type="text" name="state" size="2" maxlength="2" />

Zipcode: <input type="text" name="zip" size="5" maxlength="5" />

</p>

<hr />

<b>Type of tree:</b> <select name="treetype" >

<option value="Elm">Elm</option>

<option value="Chestnut">Chestnut</option>

<option value="Japanese Tree Lilac">Japanese Tree Lilac</option>

<option value="Sycamore">Sycamore</option>

</select>

<hr />

Please share any suggestions or comments with us: <textarea

name="comments" rows="3" cols="65">Comments?</textarea>


<hr />

<input type="submit" value="Order Tree" />

<input type="reset" value="Start Over" />

</form>

</body>

</html>
Copy linkTweet thisAlerts:
@cmbauthorFeb 23.2005 — And this is where I found the free javascript:

http://javascript.internet.com/forms/val-external-js.html#source
Copy linkTweet thisAlerts:
@Warren86Feb 23.2005 — cmb:

I'm not going to use that JS. You didn't state what a VALID entry would be, or are you just looking to make sure that none of the fields are empty, and that a choice has been made from the select list?
Copy linkTweet thisAlerts:
@cmbauthorFeb 23.2005 — I guess just so that all the fields are filled in. The state should only be 2 characters, and I think the zip code should be numberic, although the coding says "text." Again, I'm very new to this, so, any and all advice is welcome.
Copy linkTweet thisAlerts:
@Warren86Feb 23.2005 — <HTML>

<Head>

<Script Language=JavaScript>

function validate(isForm){

isValid = true;
nElements = isForm.length;
for (i=0; i<nElements; i++)
{
if (isForm[i].type == "text")
{if (isForm[i].value == ""){isValid = false}}
}
if (!isValid){alert('You must complete all fields')}
if (isValid)
{
isState = isForm.state.value;
isZip = isForm.zip.value;
if (!/[A-Z]{2}/i.test(isState)){alert('Invalid State');isValid = false}
if (!/[0-9]{5}/.test(isZip)){alert('Invalid Zip Code');isValid = false}
}
if (isValid){isForm.submit()}
}


</Script>

</Head>

<Body>

<form method="post" action="http://www.cookwood.com/cgi-bin/display_results.pl">

Name: <input type="text" name="name" />

Address: <input type="text" name="address" size="30" />

<p>City: <input type="text" name="city" />

State: <input type="text" name="state" size="2" maxlength="2" />

Zipcode: <input type="text" name="zip" size="5" maxlength="5" />

</p>

<hr />

<b>Type of tree:</b> <select name="treetype" >

<option value="" selected> Choose a tree </option>

<option value="Elm">Elm</option>

<option value="Chestnut">Chestnut</option>

<option value="Japanese Tree Lilac">Japanese Tree Lilac</option>

<option value="Sycamore">Sycamore</option>

</select>

<hr />

Please share any suggestions or comments with us: <textarea

name="comments" rows="3" cols="65">Comments?</textarea>

<hr />

<input type="button" value="Order Tree" onclick="validate(this.form)"/>

<input type="reset" value="Start Over" />

</form>

</Body>

</HTML>
Copy linkTweet thisAlerts:
@cmbauthorFeb 23.2005 — Wow! That looks like it did the trick. Since I am so new at this, how does this differ from the way I was trying to do it? (or would that take too long to explain?). I REALLY appreciate your help. It makes me feel good to know help is so close at hand. Thanks again. Cmb.
Copy linkTweet thisAlerts:
@Warren86Feb 23.2005 — Oh, I'm sorry. Most people have their own way of "doing things." I didn't even look at that other code.

But, I did forget to validate the select list, so give me a couple minutes, then come back and retrieve the updated code.
Copy linkTweet thisAlerts:
@Warren86Feb 23.2005 — You will notice that I changed the select list. The first option simply states, "Choose a tree," then I test if the user has selected another option, or not.

<HTML>

<Head>

<Script Language=JavaScript>

function validate(isForm){

isValid = true;
nElements = isForm.length;
for (i=0; i<nElements; i++)
{
if (isForm[i].type == "text")
{if (isForm[i].value == ""){isValid = false}}
}
if (!isValid){alert('You must complete all fields')}
if (isValid)
{
isState = isForm.state.value;
isZip = isForm.zip.value;
if (!/[A-Z]{2}/i.test(isState)){alert('Invalid State');isValid = false}
if (!/[0-9]{5}/.test(isZip)){alert('Invalid Zip Code');isValid = false}
}
if (isValid)
{if (isForm.treetype.selectedIndex == 0){alert('Choose a tree type');isValid = false}}
if (isValid){isForm.submit()}
}


</Script>

</Head>

<Body>

<form method="post" action="http://www.cookwood.com/cgi-bin/display_results.pl">

Name: <input type="text" name="name" />

Address: <input type="text" name="address" size="30" />

<p>City: <input type="text" name="city" />

State: <input type="text" name="state" size="2" maxlength="2" />

Zipcode: <input type="text" name="zip" size="5" maxlength="5" />

</p>

<hr />

<b>Type of tree:</b> <select name="treetype" >

<option value="" selected> Choose a tree </option>

<option value="Elm">Elm</option>

<option value="Chestnut">Chestnut</option>

<option value="Japanese Tree Lilac">Japanese Tree Lilac</option>

<option value="Sycamore">Sycamore</option>

</select>

<hr />

Please share any suggestions or comments with us: <textarea

name="comments" rows="3" cols="65">Comments?</textarea>

<hr />

<input type="button" value="Order Tree" onclick="validate(this.form)"/>

<input type="reset" value="Start Over" />

</form>

</Body>

</HTML>
×

Success!

Help @cmb 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.4,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

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