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.
@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>
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?
@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.
@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.
@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()} }