Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jan 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Boolean || operator question.

    Hi,

    Can someone please tell me what is wrong with the following form validation. It doesn't work 100% accurately. For example, the form will still submit when I type in "a" even though I have stated that values lower than 6 characters not be allowed.

    I think I must be using the || operator incorrectly. Any help would be appreciated. Thanks.



    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <SCRIPT LANGUAGE = "JavaScript">
    <!--
    function checkForm(form) {
    var reg = /\W/
    var litmus = true
    if (form.firstName.value == "" || reg.test(form.firstName.value) || form.firstName.length < 6 || form.firstName.length > 15){
    fName.style.visibility ='visible'
    litmus = false
    }else{
    fName.style.visibility = 'hidden'
    }
    return litmus
    }

    //-->
    </SCRIPT>
    </head>

    <body>

    <form onSubmit ="return checkForm(this)">

    <div id = "fName" style= "visibility:hidden">Please enter your First Name!</div>
    <p>First Name:</p>
    <input type="text" name="firstName" id="firstName" size="30" maxlength="60" />
    <input type="submit" value="Click to Submit" />

    </form>

    </body>
    </html>

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts

    Smile Mode: Standard JavaScript debugging procedure

    Ok, let's do a breakdown:
    Originally posted by krusty
    form.firstName.value == "" || reg.test(form.firstName.value) || form.firstName.length < 6 || form.firstName.length > 15
    Returns true if:
    • The value of the form field is is an empty string, OR
    • The value of the form field matches the regex, OR
    • The length of the form field is smaller than than 6, OR
    • The length of the form field is larger than 15
    Which seems to be what you want. "Why doesn't it work?" you might ask.


    Why don't you alert the length of the form field to have a look?





    Now you know it's undefined. What might the reason be?




    Yeah, you got it. It's the length of the value of the form field you want, not the length of the actual form field.
    Last edited by liorean; 01-14-2004 at 04:17 PM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    New to the CF scene
    Join Date
    Jan 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thanks for the help.

    Thanks for the help, as you can tell I'm still new to this and I was banging my head against the wall.

    Dan

  • #4
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    a point if you make an if statement that checks if something is higher than a or smaller then b and b is larger than a:

    X<a
    X>b
    b<a

    if(x<a || x>b){}


    such an if sentence will always be true therefore irrellevant or wrong:

    examples of true:

    a=3
    b=6;

    1. x=4

    x<a =true
    x>b =true
    if( true || true){}

    2. x=2
    x<a =false
    x>b =true
    if( false|| true){}

    3. x=7
    x<a = true
    x>b = false
    if( true|| false){}



    as it should be clear no matter what x is the if will always be launched, my guess if that u wanna do an and check(&&) to make sure x is between the 2 numbers.

  • #5
    New to the CF scene
    Join Date
    Jan 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    solved it

    hi,

    thanks for the responses, the suggestion from liorean solved the problem.

    Dan


  •  

    Posting Permissions

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