SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2010
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Form validation problem

    I am using a function (below) for validation. However, if the first condition is true then all the conditions after do not execute, e.g. the form doesnt alert any error messages..any ideas? (code is below)

    // chkNumeric is an external function, see at the end.

    function validate_form ( )
    {
    valid = true;

    if( ( document.xform.name.value == "" ) || ( document.xform.terms.value == "" )|| ( document.xform.terms.value=="" )
    || (document.xform.buiness_name.value=="")|| ( document.xform.location.value =="" )|| ( document.xform.min_qty.value=="" )
    || ( document.xform.max_qty.value == "") || ( document.xform.description.value == "" ) )
    {
    $.prompt ('Please complete all required fields (* is required)');
    valid = false;
    }
    if(chkNumeric(document.xform.min_qty.value)==false)
    {
    $.prompt('Minimum quantity must be a number!');
    valid=false;
    }
    if(chkNumeric(document.xform.max_qty.value)==false)
    {
    $.prompt('Maximum quantity must be a number!');
    valid=false;
    }

    return valid;
    }

    function chkNumeric(strString)
    // strString is the string passed to be checked

    // check for valid numeric strings
    {
    // strvalidchars defines the set of characters which are valid in a numeric field
    var strValidChars = "0123456789.-";
    // strings
    var strChar;
    // boolresult is the variable which returns true is string is in correct format or false if it is not a valid numeric string
    var boolResult = true;

    if (strString.length == 0) return false;
    // test strString consists of valid characters listed above
    for (i = 0; i < strString.length && boolResult == true; i++)
    {
    strChar = strString.charAt(i);
    if (strValidChars.indexOf(strChar) == -1)
    {
    boolResult = false;
    }
    }
    return boolResult;
    }

  2. #2
    SitePoint Addict Kokos's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The only reason I can see here for Javascript to stop executing would be a syntax error, but this code has no errors.

    Have you checked if $.prompt is working correctly?

    Taking over the web one pixel at a time.
    Currently working @ CodeCreators

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2010
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes $.prompt works fine, as it is executing if I submit the form and the first "if" is false, the other if's also get executed at the same time, but if the first "if" is true, the others dont get executed independantly

  4. #4
    SitePoint Enthusiast
    Join Date
    Jun 2010
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just added the other function that i was calling inside the main function

  5. #5
    SitePoint Addict Kokos's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One mistake in your chkNumeric function:

    for (i = 0; i < strString.length && boolResult == true; i++)

    i = 0, you have to declare i first in javascript, so change this line to:

    for (var i = 0; i < strString.length && boolResult == true; i++)

    Taking over the web one pixel at a time.
    Currently working @ CodeCreators

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Kokos View Post
    i = 0, you have to declare i first in javascript, so change this line to:

    for (var i = 0; i < strString.length && boolResult == true; i++)
    Since variable declarations are automatically hoisted to the top of the existing scope, it's preferred to declare all variables at the start of the function that they're in, so as to reduce any confusion.

    Code javascript:
    var i;
     
    for (i = 0; i < strString.length && boolResult == true; i++) {
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    Since variable declarations are automatically hoisted to the top of the existing scope, it's preferred to declare all variables at the start of the function that they're in, so as to reduce any confusion.

    Code javascript:
    var i;
     
    for (i = 0; i < strString.length && boolResult == true; i++) {
    Also, one of the useful benefits of placing variable declarations at the start is that it's easier to see if lots of unrelated variables are being used. If that's the case then it's a good early warning that you should split things up in to a couple of separate functions.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  8. #8
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    By the way, all of the code for chkNumeric can be replaced with a regular expression test.

    Code javascript:
    function chkNumeric(strString) {
        return !isNaN(Number(strString));
    }

    Where isNaN() and Number() are built-in methods that already do the job for you.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •