Click to See Complete Forum and Search --> : I have no brain
crispy_duck
05-02-2003, 08:57 AM
hi all,
I had to join this forum to ask for some help as I'm on my own in the office and my brain is not working.
I've got a little JS experience but am still learning , thats why I'm stuck on what should be something simple but i need help.
I have a form checking function but need to change it a little.
First part:
if (document.myForm.postcode.value == ""){
alert('Please enter a postcode');
return false;
i need to change this part so I can check for a LETER at the start of the string entered in to the INPUT box.
Second part:
if (document.myForm.contact.value == "telephone"){
if (document.myForm.phone.value == ""){
alert('If you want us to contact you via telephone, please provide your phone number');
return false;
}
I want to change this so that a number must be entered and it must start with a 0.
can anyone help, its been a long day.
thanks
Hi,
I have some java script from a site, which checks British post codes, Do not take it as read, though, I think this is the sites url http://javascript.internet.com/ you will have to do some searhing of the site, this is a great site for me, I have learnt by trial and error! I have the file on this computer somewhere coz I thought it may be useful!
Rod
:)
crispy_duck
05-02-2003, 09:24 AM
hey thanks,
i've used javascript.internet many times and its usually a great help.
I've figured out how to make sure i get a number on the phone field, now i need to make sure its a ZERO.
I'll try to find the script for the postcode.
cmelnick
05-02-2003, 09:46 AM
If you are familiar with regular expressions, they can be used with Javascript, and are quite powerful pattern matching. Try this:
The tricky part is getting the regular expression right. I don't know what the postal codes are in your area, so for the example below, the postal code must be one letter followed by 4 numbers.
The regular expression is between the two /'s. I will go through step by step explaining each element in the regular expression. Feel free to contact me and tell me your exact needs, and I can help create you a working reg exp.
So my reg exp below is ^[a-z]{1}[0-9]{4}$
The "gi" afterwords says match globally, and case insensitive.
The ^ says start matching at the beginning of the string.
[a-z] says match any character in the range between the [ and ] (so a, b, c, ..., y, z) The case insensitive will match A-Z too. The alternate way to do this would be [a-zA-Z], which will match a, b, c, ...z, A, B, C, ...Z. [a-zA-Z0-9] would match a-z, A-Z and 0-9.
{1} says match the previous pattern {x}
times.
[0-9] says match any number.
{4} says match previous pattern 4 times.
$ says match until the end of the string.
If you didn't have the ^ at the beginning, the string "a1234" would be correct, but so would "abcde1234". If you didn't have the $ at the end, "a1234" would be correct, but so would "a123456789".
See http://sitescooper.org/tao_regexps.html for more detailed information on using regular expressions.
<html>
<head>
<title>Test</title>
</head>
<body>
<form name="test" onSubmit="return checkForm();" action="">
Postal code: <input type="text" name="postCode"><br>
<input type="submit" value="Check Form / Submit">
</body>
<script language="javascript">
function checkForm() {
var regExp_postCode = /^[a-z]{1}[0-9]{4}$/gi;
if (regExp_postCode.test(document.test.postCode.value)) {
alert ("Is a valid postal code");
return true;
} else {
alert ("Is not a valid postal code");
return false;
}
}
</script>
</html>
cmelnick
05-02-2003, 09:52 AM
Originally posted by crispy_duck
hey thanks,
i've used javascript.internet many times and its usually a great help.
I've figured out how to make sure i get a number on the phone field, now i need to make sure its a ZERO.
I'll try to find the script for the postcode.
So, using my regular expression above, if you needed a phone number that started with 0 and had x numbers following, the regular expression would be:
/^0{1}[0-9]{x}$/
but replace x with how many numbers you need after the 0.
A more complicated test pattern would be to test to see if they put the "-" between sets of numbers. If they entered 0-342-3724 or 03423724, both should work. The test for this would be:
/^0{1}-?[0-9]{3}-?[0-9]{4}$/
The "?" says match the previous pattern 0 or 1 times. In this case, the previous pattern is "-".
One MORE step of complexity is I believe that European phone numbers aren't necessairly all the same length, so you could also have 0-342-3724, but you might also have 0-342-11702. In this case, just add more to the number of times to match:
/^0{1}-?[0-9]{3}-?[0-9]{4,6}$/
{4,6} says match 4, 5, or 6 times.
Send me an e-mail if you want specific help, or want me to create a reg exp for you.
Padrill
05-02-2003, 09:53 AM
Check out this link:
Expressions and Operators (http://developer.netscape.com/docs/manuals/communicator/jsguide4/expr.htm)
It's part of the Netscape JavaScript guide. Scroll down to Regular Expressions and uncover the wonderful world of pattern matching :cool:
------------
You're welcome :D
crispy_duck
05-02-2003, 09:58 AM
thanks to all.
I'm not gonna make the form checking to complex, i just want to make sure i get a ZERO at the start of the phone number filed and any letter at the start of the postcode field.
I have used this to check for a number in the phone filed:
if (parseInt(document.forms[0].phone.value)
!= document.forms[0].phone.value) {
alert('Please enter a phone number, numbers only');
return false;
}
and it works ok. I'm not used to using Reg Ex so i'm just guessing at the moment. I'll give it a try for a while.