best way to test blank data in required fields

Discussion in 'Javascript' started by Matt, Sep 16, 2004.

  1. Matt

    Matt Guest

    I want to test the blank data in required field. If the user enter
    blank data, the following code still not work. But if I test for the
    length of a string, it doesn't work also, any ideas?? thanks!!

    if (InputForm.username.value == '')
    alert("username is required");
     
    Matt, Sep 16, 2004
    #1
    1. Advertising

  2. Matt

    Hal Rosser Guest

    "Matt" <> wrote in message
    news:...
    > I want to test the blank data in required field. If the user enter
    > blank data, the following code still not work. But if I test for the
    > length of a string, it doesn't work also, any ideas?? thanks!!
    >
    > if (InputForm.username.value == '')
    > alert("username is required");


    You might check for null values as well.
    If the DB field is 'not required' it may be null.



    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.754 / Virus Database: 504 - Release Date: 9/6/2004
     
    Hal Rosser, Sep 16, 2004
    #2
    1. Advertising

  3. On 15 Sep 2004 16:33:26 -0700, Matt <> wrote:

    > I want to test the blank data in required field. If the user enter blank
    > data, the following code still not work. But if I test for the length of
    > a string, it doesn't work also, any ideas?? thanks!!


    In what way doesn't it work? Is it a silent failure, a script error?

    > if (InputForm.username.value == '')
    > alert("username is required");


    I'd guess a script error. The FAQ (<URL:http://jibbering.com/faq/>) shows
    a better way to access the value of a form control.

    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.
     
    Michael Winter, Sep 16, 2004
    #3
  4. Matt

    Matt Guest

    > In what way doesn't it work? Is it a silent failure, a script error?
    >
    > > if (InputForm.username.value == '')
    > > alert("username is required");

    >
    > I'd guess a script error. The FAQ (<URL:http://jibbering.com/faq/>) shows
    > a better way to access the value of a form control.
    >
    > Mike


    If the user enter many whitespaces, it won't work
     
    Matt, Sep 16, 2004
    #4
  5. On 16 Sep 2004 09:50:07 -0700, Matt <> wrote:

    [snip]

    >>> if (InputForm.username.value == '')
    >>> alert("username is required");


    [snip]

    > If the user enter many whitespaces, it won't work


    Then I doubt the problem here is the script, but your definition of
    'working'.

    As long as validation fails if the user leaves the field completely blank,
    the script works exactly as it should do. The issue here is that it
    doesn't do what you *want* it to.

    If you want to exclude all whitespace characters, and ensure that there is
    at least one non-whitespace character, then use a regular expression:

    var form = document.forms['InputForm'],
    user = form.elements['username'];

    if(!/^\S+$/.test(user.value)) {
    alert('Please enter a valid user name.');
    }

    If you want to ensure a minimum number of non-whitespace characters, then
    replace the plus (+) with {n,m}, where n is the minimum number, and m is
    the maximum. If you omit the latter, there is no maximum limit. For
    example,

    /^\S{1,10}$/ 1-10 non-whitespace characters
    /^\S{7,}$/ at least seven
    /^\S{10}$/ exactly ten

    Hope that helps,
    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.
     
    Michael Winter, Sep 16, 2004
    #5
  6. Matt

    simina Guest

    It could be a minor reason but a big trouble...
    So:
    1 Check for the name of the field. Is it really usesrname or userName
    or whatever...
    2 Chech the same field from the database: is it a required field?
    'cause if it is, you should maybe check for null, too.
    3 Try a verification for "undefined" and test again with an alert
    message.
    4 Do you call the function properly?

    Hope it helps...
     
    simina, Sep 16, 2004
    #6
  7. Matt wrote:
    > I want to test the blank data in required field. If the user enter
    > blank data, the following code still not work. But if I test for the
    > length of a string, it doesn't work also, any ideas?? thanks!!
    >
    > if (InputForm.username.value == '')
    > alert("username is required");

    Hi Matt,

    Generally, your code should work. I would say "InputForm" is not the
    handle of your form object.

    How did you get to the code above?

    <form name="InputForm" action="whatever" method="post" onsubmit="return
    checkFields(this);"> ?

    Then
    function checkFields(form){
    if(form.username.value == ""){
    alert("Fill in the user name");
    return false;
    }
    return true;
    }

    You should have a return false; if the field is not filled in.

    Chris
     
    Antonie C Malan Snr, Sep 26, 2004
    #7
  8. Checking image size

    I have an application uploading image files to the server. They have to
    be of a specific size. How do I reliably check them on the client side?

    I did:

    if(form.photo3.value != ""){
    var img = form.photo3.value;
    var piccie = new Image();
    piccie.src = img;
    var h = piccie.height;
    var w = piccie.width;
    if(h > 244 || w > 324){
    alert("Your photo 3 is " + w + " pixels wide and " + h + " pixels
    high.\n The required dimensions are 320w x 240h. Please scale your
    picture");
    return false;
    }

    This sometimes works and sometimes does not. I tried piccie.src =
    "file://" + img. This also works unreliably.

    Any ideas?

    Thanks,

    Chris
     
    Antonie C Malan Snr, Sep 26, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    10
    Views:
    742
    Joona I Palaste
    Sep 18, 2004
  2. Robert Kattke
    Replies:
    0
    Views:
    449
    Robert Kattke
    Dec 31, 2003
  3. NetHead
    Replies:
    1
    Views:
    269
    Marc 'BlackJack' Rintsch
    Jul 24, 2007
  4. Skybuck Flying

    test(void *data) vs test(void &data)

    Skybuck Flying, Jun 18, 2011, in forum: C Programming
    Replies:
    91
    Views:
    1,670
    Shao Miller
    Jun 27, 2011
  5. oldyork90
    Replies:
    1
    Views:
    175
    Jeremy J Starcher
    Sep 10, 2008
Loading...

Share This Page