SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member poothai's Avatar
    Join Date
    Aug 2003
    Location
    Bangkok
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    can you check my syntax, please...

    assumed that my form onsubmit will call func checkform ,

    and I have a field that will check user input, only if the field is not blank the data format input must contain "http" and(gif or jpg) this is mean users must enter full url of picture if they use is form field, ok ?

    Code:
    function CheckForm () {
    var errorMsg = "";
           if (document.frmAddMessage.apicurl.value.length >0 && 
          (document.frmAddMessage.apicurl.value.indexOf("jpg",0) ==-1 || 
          document.frmAddMessage.apicurl.value.indexOf("gif",0) ==-1)) {
           errorMsg += "\n\t- pic url\tShould be full url with gif or jpg extension";
           }
    
    if (errorMsg != ""){
             msg = "blah blah blah\n\n";
             errorMsg += alert(msg + errorMsg + "\n\n");
    return false;
             }
    return true;
             }
    i dunno what's wrong, it's alway return false even it should true ... if I leave this field blank it's true, but if not blank it's alway false even i enter the correct url format ...

    where is the fault ???

  2. #2
    SitePoint Enthusiast kgish's Avatar
    Join Date
    Dec 2001
    Location
    Gouda, The Netherlands
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hope this helps:

    Code:
     function CheckForm () {
     var rc = false; // until proven otherwise
     var val = document.frmAddMessage.apicurl.value;
     var len = val.length;
     if (len > 4) {
       rc = val.indexOf(".jpg") == len - 4 || val.indexOf(".gif") == len - 4 ;
     }
     
     if (!rc) { alert(rc); }
     return rc;
    }
    Rather than just checking whether or not either "jpg" or "gif" appear somewhere in the string, I validate that it indeed appears as an extension, eg. at the end of the string.

    Hint: Whenever I get these kinds of vague and bothersome problems, I always simplify the code as much as possible.
    Kiffin
    Your average future-famous kind of guy...

  3. #3
    SitePoint Member poothai's Avatar
    Join Date
    Aug 2003
    Location
    Bangkok
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much for great tut

    .. how dumb i am

    ieieie

  4. #4
    SitePoint Enthusiast kgish's Avatar
    Join Date
    Dec 2001
    Location
    Gouda, The Netherlands
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're not dumb at all. Even the geekiest Javascript gurus need a helping hand once in awhile!
    Kiffin
    Your average future-famous kind of guy...

  5. #5
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    You might also consider passing the field object when you make the function call. That way, you can use the same function for more than one field.
    Code:
    if from within a function:
     
    someVar = CheckForm(document.add....)
     
    if from within a textbox:
     
    <input... onBlur='CheckForm(this)'>
     
    if from a button:
     
    <input ... onclick="CheckForm(this.form[fieldName]">
    function CheckForm (fldObj) 
    { 
    var rc = false; // until proven otherwise var val = fldObj.value;
    var len = val.length; if (len > 4)
    ...

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  6. #6
    SitePoint Enthusiast kgish's Avatar
    Join Date
    Dec 2001
    Location
    Gouda, The Netherlands
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sure Vinny, it becomes a more elegant solution by extra parameters to make it generic and flexible.

    But also remember that the more general the solution the more prone it is to error.

    That was the original pitfall of the initial solution I feel, and that is why I go for simplicity.
    Kiffin
    Your average future-famous kind of guy...


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
  •