SitePoint Sponsor

User Tag List

Results 1 to 15 of 15

Thread: || or

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Mar 2011
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb || or

    Can anyone think why this wouldn't work?

    It needs to validate a 4 digit postcode

    I think im doing something wrong with ||
    function numVal()
    {
    var pc = document.Forders.postcode.value;
    if(isBlank(pc) || !isNum(pc) || pc.length != 4)
    {
    alert("Please enter a valid Post Code");
    document.Forders.postcode.focus();
    }
    }

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,789
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Have you defined isBlank and isNum?
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Zealot
    Join Date
    Mar 2011
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Red face

    Quote Originally Posted by felgall View Post
    Have you defined isBlank and isNum?
    what do you mean set those two functions, im so confused?

    any help would be appreciated

  4. #4
    SitePoint Zealot
    Join Date
    Mar 2011
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    Im trying to write a function like this;

    function post()
    {
    var x=document.Forders.postcode.value;
    if( x isnt a num || x isnt equal to 4 characters/num in legth)
    then
    alert(invalid postcode)
    }
    Except actually in javascript
    Im really new to javascript a i am getting there though.

    any suggestions/leads?

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,789
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Well if you added those two functions then the code in your first post would work.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  6. #6
    SitePoint Zealot
    Join Date
    Mar 2011
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    He's referring to this line here:

    Code:
    if(isBlank(pc) || !isNum(pc) || pc.length != 4)
    Those functions do not normally exist, so you need to create those functions.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  8. #8
    SitePoint Zealot
    Join Date
    Mar 2011
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    okay ive set up those functions and it all works

    except for the pc.length != 4 bit which i thought was the most straight forward part!!

    any ideas?

  9. #9
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by minusten View Post
    okay ive set up those functions and it all works

    except for the pc.length != 4 bit which i thought was the most straight forward part!!

    any ideas?
    Youre right, that is strange. A sample test page wouldn't hurt, so we can tell exactly what's going on there.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  10. #10
    SitePoint Zealot
    Join Date
    Mar 2011
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is this what you mean by sample page? Its just the js


    function numVal() //number validation master
    {
    var pc = document.Forders.postcode.value;
    if(isBlank(pc) || !isNum(pc) || pc.length != 4)
    {
    alert("Please enter a valid Post");
    document.Forders.postcode.focus();
    }
    var hp = document.Forders.homeP.value;
    if(isBlank(hp) || !isNum(hp) || hp.length !=10)
    {
    alert("Please enter a valid contact number");
    document.Forders.homeP.focus();
    }
    }

    function isNum()// number validation is number
    {
    if(isNaN(document.Forders.postcode.value))
    {
    alert("Only numbers are allowed.");
    Forders.postcode.focus();
    return false;
    }
    if(isNaN(document.Forders.homeP.value))
    {
    alert("Only numbers are allowed.");
    Forders.homeP.focus();
    return false;
    }
    }
    function isBlank()//number validation isnt blank
    {
    if (document.Forders.postcode.value.length == 0 )
    {
    alert("Please enter your postcode");
    return false;
    }
    if (document.Forders.homeP.value.length == 0 )
    {
    alert("Please enter a valid contact number");
    return false;
    }
    }

  11. #11
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    I don't know where to begin, I'm sorry.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  12. #12
    SitePoint Zealot
    Join Date
    Mar 2011
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks anyway

  13. #13
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Let's give it a try.

    The isNum() function and the isBlank() function are supposed to receive their values via the function arguments. They should not reach out and control things that are not given to them.

    So for example, the isBlank function could be as simple as:

    Code javascript:
    function isBlank(str) {
        return str === '';
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  14. #14
    SitePoint Zealot
    Join Date
    Mar 2011
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for replying

    don't worry about those functions i can fix those functions up, any idea on the length not equal to 4 thing?

  15. #15
    SitePoint Zealot
    Join Date
    Mar 2011
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i got it there was a!in the wrong spot!!


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •