Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

javascript check post code

Avatar of peter2001
peter2001 asked on
JavaScriptPHP
3 Comments1 Solution1065 ViewsLast Modified:
am using the code below to check if the post code is in the right format
but i would also like to allow the field to be left empty if they don't know the post code

so if the field .Post_code is empty then return true as well and let the form submit

on my form submit am using     onclick="return postit()"  

what would be the best way to do this

<SCRIPT LANGUAGE="JavaScript">
 
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->
<!-- Original code by Peter Haydon -->
<!-- peter_haydon@lineone.net -->
 
<!-- Begin
function postit(){ //check postcode format is valid
 test = document.update_listing.Post_code.value; size = test.length
 test = test.toUpperCase(); //Change to uppercase
 while (test.slice(0,1) == " ") //Strip leading spaces
  {test = test.substr(1,size-1);size = test.length
  }
 while(test.slice(size-1,size)== " ") //Strip trailing spaces
  {test = test.substr(0,size-1);size = test.length
  }
 document.update_listing.Post_code.value = test; //write back to form field
 if (size < 6 || size > 8){ //Code length rule
  alert(test + " is not a valid postcode - wrong length");
  document.update_listing.Post_code.focus();
  return false;
  }
 
 
 if (!(isNaN(test.charAt(0)))){ //leftmost character must be alpha character rule
   alert(test + " is not a valid postcode - cannot start with a number");
   document.update_listing.Post_code.focus();
   return false;
  }
 if (isNaN(test.charAt(size-3))){ //first character of inward code must be numeric rule
   alert(test + " is not a valid postcode - alpha character in wrong position");
   document.update_listing.Post_code.focus();
   return false;
  }
 if (!(isNaN(test.charAt(size-2)))){ //second character of inward code must be alpha rule
   alert(test + " is not a valid postcode - number in wrong position");
   document.update_listing.Post_code.focus();
   return false;
  }
 if (!(isNaN(test.charAt(size-1)))){ //third character of inward code must be alpha rule
   alert(test + " is not a valid postcode - number in wrong position");
   document.update_listing.Post_code.focus();
   return false;
  }
 if (!(test.charAt(size-4) == " ")){//space in position length-3 rule
   alert(test + " is not a valid postcode - no space or space in wrong position");
   document.update_listing.Post_code.focus();
   return false;
   }
 
 
 
 
 count1 = test.indexOf(" ");count2 = test.lastIndexOf(" ");
 
 
 
 
 if (count1 != count2){//only one space rule
   alert(test + " is not a valid postcode - only one space allowed");
   document.update_listing.Post_code.focus();
   return false;
  }
alert("Postcode Format OK");
return true;
}
//  End -->
</script>
ASKER CERTIFIED SOLUTION
Avatar of Valleriani
VallerianiFlag of Sweden image

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Commented:
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answers