/    Sign up×
Community /Pin to ProfileBookmark

I have no brain

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

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@D_ohMay 02.2003 — 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

?
Copy linkTweet thisAlerts:
@crispy_duckauthorMay 02.2003 — 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.
Copy linkTweet thisAlerts:
@cmelnickMay 02.2003 — 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}$
[list]
  • [*]The "gi" afterwords says match [b]g[/b]lobally, and case [b]i[/b]nsensitive.

  • [*]The [b]^[/b] says start matching at the beginning of the string.

  • [*][b][a-z][/b] says match any character in the range between the [b][[/b] and [b]][/b] (so a, b, c, ..., y, z) The case insensitive will match A-Z too. The alternate way to do this would be [b][a-zA-Z][/b], which will match a, b, c, ...z, A, B, C, ...Z. [b][a-zA-Z0-9][/b] would match a-z, A-Z and 0-9.

  • [*][b]{1}[/b] says match the previous pattern [b]{[/b]x[b]}[/b]

    times.

  • [*][b][0-9][/b] says match any number.

  • [*][b]{4}[/b] says match previous pattern 4 times.

  • [*][b]$[/b] says match until the end of the string.

  • [/list]


    If you didn't have the [b]^[/b] at the beginning, the string "a1234" would be correct, but so would "abcde1234". If you didn't have the [b]$[/b] 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.

    <i>
    </i>
    &lt;html&gt;
    &lt;head&gt;
    &lt;title&gt;Test&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
    &lt;form name="test" onSubmit="return checkForm();" action=""&gt;
    Postal code: &lt;input type="text" name="postCode"&gt;&lt;br&gt;
    &lt;input type="submit" value="Check Form / Submit"&gt;
    &lt;/body&gt;
    &lt;script language="javascript"&gt;

    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;
    }

    }
    &lt;/script&gt;

    &lt;/html&gt;
    Copy linkTweet thisAlerts:
    @cmelnickMay 02.2003 — [i]Originally posted by crispy_duck [/i]

    [B]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. [/B]
    [/QUOTE]



    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.
    Copy linkTweet thisAlerts:
    @PadrillMay 02.2003 — Check out this link:

    [URL=http://developer.netscape.com/docs/manuals/communicator/jsguide4/expr.htm]Expressions and Operators[/URL]

    It's part of the Netscape JavaScript guide. Scroll down to Regular Expressions and uncover the wonderful world of pattern matching ?


    ------------

    You're welcome ?
    Copy linkTweet thisAlerts:
    @crispy_duckauthorMay 02.2003 — 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.
    ×

    Success!

    Help @crispy_duck 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 5.3,
    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,
    )...