/    Sign up×
Community /Pin to ProfileBookmark

Javascript form validation problem

Hey guys im new to javascript and having a few problems with a form validation script. Its supposed to stop sending the form if key fields are missing, but it just sends them anyway!

below is the code i use in the header to check for blank fields:

[code]function checkForm(f)
{
if (f.elements[‘Email’].value == “” && f.elements[‘Telephone’].value == “”)
{
alert(“You have not completed the form correctly, please fill out ALL fields”);
return false;
}
else
{
f.submit();
return false;
}
}[/code]

and now the code i use to action it:

[code] <form method=”post” action=”contactprocess.php” name=”sform” onSubmit=”return checkForm(this); return false;”>[/code]

from what i can see the fields match, it all links up correctly but still it will allow blank forms to be sent ?

your help would be wonderful!

Thanks

DK

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@twseitexAug 14.2011 — Hi,

<form

method="post"

action="contactprocess.php"

name="sform"

onSubmit="return checkForm(this); return false;" --> 2x return
>



onsubmit="return(checkForm(this))"

example for onsubmit see http://msdn.microsoft.com/en-us/library/ms536972(v=VS.85).aspx





elements Collection see http://msdn.microsoft.com/en-us/library/ms537449.aspx

only object FORM
--------------------------------------------------------------------------------



Retrieves a collection, in source order, of all controls in a given form. input type=image objects are excluded from the collection.

Syntax

[ oColl = ] FORM.elements

[ oObject = ] FORM.elements(vIndex [, iSubIndex])

Possible Values

oColl Array of button, input, select, and textArea objects.


oObject Reference to an individual item in the array of elements contained by the object.

vIndex Required. Integer or string that specifies the element or collection to retrieve. If this parameter is an integer, the method returns the element in the collection at the given position, where the first element has value 0, the second has 1, and so on. If this parameter is a string and there is more than one element with the name or id property equal to the string, the method returns a collection of matching elements.

iSubIndex Optional. Position of an element to retrieve. This parameter is used when vIndex is a string. The method uses the string to construct a collection of all elements that have a name or id property equal to the string, and then retrieves from this collection the element at the position specified by iSubIndex.

Property Description

length Sets or retrieves the number of objects in a collection.


Method Description

item Retrieves an object from the all collection or various other collections.

namedItem Retrieves an object or a collection from the specified collection.

tags Retrieves a collection of objects that have the specified HTML tag name.


urns Retrieves a collection of all objects to which a specified behavior is attached.

Remarks

This collection can contain any combination of the button, input, select, and textArea objects. For compatibility with existing Web content, however, input type=image objects are excluded.

A complete list of form controls is provided in the Introduction to Forms.

Standards Information

This collection is defined in World Wide Web Consortium (W3C) Document Object Model (DOM) Level 1 .
Copy linkTweet thisAlerts:
@deamon2kauthorAug 14.2011 — um ok....

fixed it myself - just had to change operators and use in instead of and.

as for what twseitex had posted...

not really relevant in solving my question or answering my question in any way.

Pretty useless...
Copy linkTweet thisAlerts:
@twseitexAug 14.2011 — Hi,

two returns in a event handler .... aja.

and this is correctly too ?

function checkFormHandler()

{

return checkForm(document.getElementById("idForm"));

return false;

}

document.getElementById("idForm").onsubmit=checkFormHandler; // better atacheEvent etc.

aja :-))

yep pretty useless your code without elements 'Telephone' etc. of form ... very good for explain of checkForm(f) :-))
×

Success!

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