#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    292
    Rep Power
    0

    Trying to combine two different javascript functions


    One is a Macromedia function that checks for blank fields and fields with spaces only. The other checks to see if a username already exists.

    They both work, on their own. But I'm trying to figure out how to call them all together so it will check for:

    - blank fields
    - fields with only spaces
    - check for existing username


    Here's the seperated code:
    PHP Code:

    MACROMEDIA 
    FUNCTION:

    function 
    MM_validateForm() { //v4.0
      
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
      for (
    i=0i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
        if (
    val) { nm=val.name; if (((val=val.value)!="")&&(!/^\s+$/.test(val))) {
          if (
    test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
            if (
    p<|| p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
          } else if (
    test!='R') { num parseFloat(val);
            if (
    isNaN(val)) errors+='- '+nm+' must contain a number.\n';
            if (
    test.indexOf('inRange') != -1) { p=test.indexOf(':');
              
    min=test.substring(8,p); max=test.substring(p+1);
              if (
    num<min || max<numerrors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
        } } } else if (
    test.charAt(0) == 'R'errors += '- '+nm+' is required.\n'; }
        } if (
    errorsalert('The following error(s) occurred:\n'+errors);
      
    document.MM_returnValue = (errors == '');
    }


    MY FUNCTIONS:
    function 
    checkUsername(){
       
    document.checkimg.src='users_response.php?u='+escape(document.form1.username.value);
    }

    function 
    response(exists){
     if (!
    exists&&document.form1.username.value != "") {
         
    document.form1.submit();
     }
     else if (
    document.form1.username.value != "") {
        
    alert('That username already exists.\nPlease choose another.');
     }


    The two submit buttons I have to test each one are:

    PHP Code:
    this calls the macromedia function:
    <
    input name="Submit" type="submit" id="Continue3" onClick="MM_validateForm('username','','R','password','','R');return document.MM_returnValue" value="Submit">
            
    this one calls the checkUsername function:
    <
    input name="Submit" type="submit" value="Submit" onClick="checkUsername();return false" 
    Is there a way to combine these so upon clicking ONE submit button, it runs all the above functions? Or somehow steps through them?


    I tried something like:
    PHP Code:

    onClick
    ="MM_validateForm('username','','R','password','','R');return document.MM_returnValue;checkUsername();return false" value="Submit"
    With no luck. Any ideas?
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Oct 2001
    Location
    New Zealand
    Posts
    1,774
    Rep Power
    25
    How about simply putting:

    checkUsername()

    at the end of the first function?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    292
    Rep Power
    0
    Originally posted by binky
    How about simply putting:

    checkUsername()

    at the end of the first function?

    For some reason that wasn't working, but I did get it to work by calling the first function in the second one:

    MM_validateForm('username','','R','password','','R');


    As long as it works...
  6. #4
  7. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Oct 2001
    Location
    New Zealand
    Posts
    1,774
    Rep Power
    25
    Yup... as long as it works, and er that you don't tell your employers that you don't understand the reason behind it.

  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    292
    Rep Power
    0
    Originally posted by binky
    Yup... as long as it works, and er that you don't tell your employers that you don't understand the reason behind it.

    Heheh... of course I know how it works....

IMN logo majestic logo threadwatch logo seochat tools logo