DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: javascript validation error message

  1. #1
    Join Date
    Apr 2007
    Posts
    42

    javascript validation errors and problems

    Can someone please tell me how to alter this code if it needs altering, . thanks in advance.


    1. The form keeps focusing on "username" and won't submit. The 2 username functions may be clashing? i want the username to be letters and numbers only , no special characters or spaces, 5-18 characters.

    2. The country is a drop down list, and they must select one. I think since it is a drop down i don't need the function which checks for blanks and empty? or do i? or maybe it should be altered?

    3. The opt15 function if for a mandatory checkbox,


    4.. That the closing brackets at the end are correct?


    Code:
     if (String(frm.username.value).replace(/ /g, "") == "")  {
             alert("Please enter desired username. Must be between 5-25 characters (no special characters).")
             document.frm.username.focus() ;
            return false; }
      
      var regExp = /^[A-Za-z][A-Za-z0-9_]*$/;
       if(!regExp.test(frm.username.value))
         alert("no special characters or spaces in 'username'")
       if(frm.username.value.length > 18 || frm.username.value.length < 5)
    alert("'Username' should be between 5-18 characters.")
     for(i=0;i<frm.username.value.length;i++) {
     document.frm.username.focus() ;
       return false; }
    
    
    if (String(frm.country.value).replace(/ /g, "") == "")  {
             alert("Please enter your country.")
             document.frm.country.focus() ;
            return false; }
      
      if(document.frm.country.value.length < 2) {  
       alert("Minimum 2 characters for 'country'.")
      document.frm.country.focus() ;
       return false; }
       
       if(document.frm.country.value.length > 20) {  
       alert("Maximum 20 characters allowed for 'country'.")
      document.frm.country.focus() ;
       return false; }
       
       var mycheckbox = document.frm.opt15; 
    if (mycheckbox.checked == false) { 
       alert("this checkbox is mandatory") 
       return false; 
    } 
       return true; 
        } 
    }
    Last edited by karen987; 04-29-2007 at 04:33 AM.

  2. #2
    Join Date
    Jun 2004
    Location
    Pakistan
    Posts
    292
    I am not JavaScript expert, in fact I am a "nub" but I think your problem might be in this part of the code (end of second block)
    Code:
    for(i=0;i<frm.username.value.length;i++) {
     document.frm.username.focus() ;
       return false; }
    this statement will always put the focus on username control and will always return false, and hence the form will not submit.
    new to programming but getting ther

  3. #3
    Join Date
    Apr 2007
    Posts
    42
    Quote Originally Posted by kashif_82
    I am not JavaScript expert, in fact I am a "nub" but I think your problem might be in this part of the code (end of second block)
    Code:
    for(i=0;i<frm.username.value.length;i++) {
     document.frm.username.focus() ;
       return false; }
    this statement will always put the focus on username control and will always return false, and hence the form will not submit.

    the last 2 lines can't be the problem, as they are in all the statements, and on other pages i use on the website.

    it's the first line that begins with "for" that is the problem i think

  4. #4
    Join Date
    Jun 2004
    Location
    Pakistan
    Posts
    292
    well this entire FOR block is the problem. This block isn't doing anything except return false and setting the focus. what happens when you take it out?
    Also your regular expression and form length if statements are missing the setfocus and return false part.
    new to programming but getting ther

  5. #5
    Join Date
    Apr 2007
    Posts
    42
    thank you kashif,

    it works now.

Similar Threads

  1. Replies: 1
    Last Post: 04-29-2007, 02:30 PM
  2. javascript form validation
    By john83 in forum Web
    Replies: 1
    Last Post: 04-19-2005, 06:27 PM
  3. JavaScript form validation
    By Bob Rouse in forum Web
    Replies: 2
    Last Post: 11-18-2002, 11:18 AM
  4. Replies: 4
    Last Post: 10-11-2001, 05:15 PM
  5. Sample Sites.
    By Murray Foxcroft in forum Web
    Replies: 5
    Last Post: 11-02-2000, 03:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center