Jump to content

easy javascript question


Recommended Posts

Hi all,

 

I found a handy form validation javascript here:

http://www.virtualpromote.com/tools/javasc...ssn=123-44-5555

 

I want to use it to validate the following form:

http://www.delijn.be/noindex/overheden/contactform.html

 

But I need 1 more validator 'type', namely to detect non-empty fields. I need to make sure 3 fields are non-empty and 1 that has to contain a correct email-address (already working).

 

Could someone tell me how I can extend the script to include this. I'd do this myself, but I'm very bad at Javascript and I don't have a lot of time to do it.

 

I know Javascript is supposed to be easy, but this is making my brain spin everytime I look at it.

Link to comment
Share on other sites

I can't seem to work this out.

 

To clarify, it does not matter which code, as long as these validations are performed when the form is submitted:

 

The form field "Naam instantie*" has to be non-empty

The form field "Gemeente*" has to be non-empty

The form field "Postcode*" has to be non-empty

 

(second part)

The form field "Naam*" has to be non-empty

The form field "Vooraam*" has to be non-empty

The form field "e-mai*" has to be non-empty and contain a valid e-mail address.

 

The form should not be submitted before all of these conditions are met.

 

So... any ideas? Tips?

Link to comment
Share on other sites

I can't actually get your form page, I get a 404.

 

Is there a reason for the * in the field names? I can't be certain, but I would think javascript doesn't like special characters like this (it is also the arithmetic multiplication operator).

 

If you only need content-validation for one field, don't bother with the linked script - it's far more than you need. Here's a simple function to check a field is non-empty:

 

 

function isNonEmpty(field) {
if(field.value=='') {
 alert('The field '+field.name+' must be filled!');
 return false;
 }
}

 

Then you could check through all the fields like so:

 

function isValid(form) {
var checks = new Array('naam instantie','Gemeente','etc','blah');
for(i=0;i<checks.length;i++)
 if(!isNonEmpty(document.getElementById(checks[i]))
   return false;
var goodMail = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;
if(!goodMail.test(form.email.value)) {
 alert('Email field is not valid.');
 return false;
 }
}

 

Then add the handler to your FORM tag:

 

 

<form name="myForm" action="nextpage.shtml" onsubmit="isValid(this);">

Link to comment
Share on other sites

I can't actually get your form page, I get a 404.

 

yeah, it is called contactform_2.html now. Decided renaming my file would be better than cleaning a couple of people's browser cache.

 

Anyway, I take a quick tour at W3schools and found a simple solution myself. Thanks for reply and especially for the e-mail validation function, I was still looking for that :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...