Results 1 to 6 of 6
  1. #1
    yuchuang is offline Member
    Join Date
    Apr 2007
    Location
    Singapore
    Posts
    29
    Rep Power
    0

    Default javascript validation

    Hi everyone, i have a problem here that i'm not sure how to solve it.
    I have the following javascript to check that it is not empty and it is not more than 2000 characters.

    Java Code:
    function confirmSubmit()
      {
      var obj = document.ResearchPaperForm;
      var check = false;
           if (obj.summary.value == ''){
                summary.innerHTML = "<span class=fail><bean:message key="alert.msg.publisher.summary.required"/></span>"        
                obj.summary.focus();
                check = false;
           }
           if (obj.summary.value.length > 2000){
                summary.innerHTML = "<span class=fail><bean:message key="alert.msg.publisher.summary.exceed"/></span>"        
                obj.summary.focus();
                check = false;
           }else {
                check = true;
           }
           if (check) {
                var r = confirm("<bean:message key="alert.msg.publisher.confirm.publish"/>")
                if (r)
                {
                    document.ResearchPaperForm.submit();
                }
                else
                {
                    check = false;
                }
            }
       }
    When i checked for empty fields, it's ok. But when i checked more than 2000 characters, it can detect the error but when i click submit, it still pop up the confirmation message even when it indicate the field cannot be more than 2000 characters. How do i prevent it? Anyone knows. Please help. Thank you.

    I have another question. If i want to validate the attachment filesize cannot be more than 10MB, how do i do it? Thanks!
    Last edited by yuchuang; 05-11-2007 at 06:43 AM.

  2. #2
    sandor is offline Member
    Join Date
    Apr 2007
    Location
    Pennsylvania,USA
    Posts
    45
    Rep Power
    0

    Default

    Just curious what this section is doing?
    Java Code:
     if (check) {
        var r = confirm("<bean:messag
        key="alert.msg.publisher.confirm.publish"/>")
          if (r)
           {
               document.ResearchPaperForm.submit();
            }
               else
                {
                    check = false;
                }
            }
    If check is already true at that point why not just submit? What is if(r) doing?
    My best method to troubleshoot jasvascript is just put alerts in there as an attempt to step thru the code.

  3. #3
    yuchuang is offline Member
    Join Date
    Apr 2007
    Location
    Singapore
    Posts
    29
    Rep Power
    0

    Default

    oh, the reason i put that section of the codes is to create a pop up message window to ask the user whether they want to confirm publish. So if r is true then it will submit the form, which is when we click ok at the pop up window. Otherwise, if user clicks cancel, the check is false and nothing will happen.

  4. #4
    sandor is offline Member
    Join Date
    Apr 2007
    Location
    Pennsylvania,USA
    Posts
    45
    Rep Power
    0

    Default

    Well I copied your code to a jsp and it works fine for me. The form submits if the value is not null and does not exceed 2000 characters. I am using firefox.
    Try with a different browser and see if it happens. Also firefox has a nice javascript console.

  5. #5
    yuchuang is offline Member
    Join Date
    Apr 2007
    Location
    Singapore
    Posts
    29
    Rep Power
    0

    Default

    Thanks! But i have another questions. How to use javascript to check the file size of the attachment we uploaded? If i don't use javascript, then can i use java codings to do it? I'm using struts. Anyone knows? Please help. Thank you.

  6. #6
    sandor is offline Member
    Join Date
    Apr 2007
    Location
    Pennsylvania,USA
    Posts
    45
    Rep Power
    0

Similar Threads

  1. Struts validation
    By Jack in forum Web Frameworks
    Replies: 3
    Last Post: 10-06-2008, 01:54 AM
  2. form validation
    By abhiN in forum New To Java
    Replies: 0
    Last Post: 03-07-2008, 08:04 AM
  3. Swing Validation
    By ppayal in forum AWT / Swing
    Replies: 0
    Last Post: 02-09-2008, 10:00 AM
  4. Swing validation
    By ppayal in forum New To Java
    Replies: 0
    Last Post: 02-09-2008, 09:59 AM
  5. Another Query On Validation
    By Albert in forum Web Frameworks
    Replies: 1
    Last Post: 07-05-2007, 07:47 PM

Posting Permissions

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