Javascript Form - if input enabled then...

Soldato
Joined
30 Nov 2005
Posts
3,084
Location
London
Hey all,

Got a really complicated form with javascript validation that I'm having to bolt on some more validation.

Essentially, a form input field is disabled until a checkbox is ticked then it becomes enabled. This all works fine.

Now I need to check ONCE then input filed is enabled it is NOT left blank or with just whitespace entered.

I've tried:

Code:
function checkall(contactform)
{
if (document.contactform.Telenumber.disabled = false) 
  { 
  if (document.contactform.Telenumber.value == "")
		{
		alert("You must enter a Tele number.");
		return (false);
		}
  }
}

Any ideas what's wrong here?
 
You can use a regex to remove whitespace and check if the field is still empty:

Code:
function checkall(contactform) {
	if (!document.contactform.Telenumber.disabled) { 
		if (document.contactform.Telenumber.value.replace(/\s/g, '') == "") {
			alert("You must enter a Tele number.");
			return (false);
		}
	}
}

Or if you use jQuery with the vlaidate plugin this whole thing become a one liner:

Code:
$(function() {
	$("#contactform").validate();
})
All you need to do is put the class 'required' on the field. The plugin will do everything else for you. There's also other methods you can use to check for minlength, email address format etc.
 
Back
Top Bottom