aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes multiple javascript functions are not working properly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "multiple javascript functions are not working properly" Watch "multiple javascript functions are not working properly" New topic
Author

multiple javascript functions are not working properly

Tushar Bhaware
Ranch Hand

Joined: Aug 13, 2012
Posts: 62

Hi all,
I am developing a website.Everything is working fine except my registration page. I want to apply validations on my fields.I have written functions accordingly.
They are working fine when i call one function at a time.Problem arises when i call multiple functions inside onsubmit or when i call wrapper function inside onsubmit.

Problem is that when i don't write anything inside any field, it does not focus on that element and shows alert messages of that field and other field as well and control moves to login page.
I hope you understand my problem. Please Help me.
I have put my both pages code here.

1.Login Page (which is working fine)


and here is Register page (where problem with java script)


Thanks in advance


Remember you have not inherited earth from your ancestor,you only borrowed it from your descendants.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61428
    
  67

Welcome to the Ranch.

Wow -- that's way too much code to look through. Besides -- it's full of JSP markup which is not helpful for a JavaScript question. (And using scriptlets? Please read this JspFaq entry).

Please post an SSCCE (Short Self-Contained Correct Example) that shows the issue.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
In html4, ids can not be numbers or start with a number, looks like you are using an HTML5 doc type so not a big issue. Using ints for ids is bad practice since it gives no reference to what the element is exactly, give it a meaningful name.

Another issue is you should be using a string with getElementById



Eric

Tushar Bhaware
Ranch Hand

Joined: Aug 13, 2012
Posts: 62

Bear Bibeault wrote:Welcome to the Ranch.

Wow -- that's way too much code to look through. Besides -- it's full of JSP markup which is not helpful for a JavaScript question. (And using scriptlets? Please read this JspFaq entry).

Please post an SSCCE (Short Self-Contained Correct Example) that shows the issue.


Thank you for your reply ... I have read why not use scriptlets. I will learn JSTL and EL.Please give me some references or names of books where i can learn about JSTL and EL
And also let me know should i start a new thread with only javascript or should i write it in another comment.
Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61428
    
  67

The JavaScript question should be handled here. Questions on JSP should be posted in the JSP forum.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Also



b can never be null, but you are checking for it in the if.

You are calling submit onclick and onsubmit, no reason for that.

Eric
Tushar Bhaware
Ranch Hand

Joined: Aug 13, 2012
Posts: 62

Eric Pascarello wrote:In html4, ids can not be numbers or start with a number, looks like you are using an HTML5 doc type so not a big issue. Using ints for ids is bad practice since it gives no reference to what the element is exactly, give it a meaningful name.

Another issue is you should be using a string with getElementById



Eric



Thank you Eric,
I will remember that but thing is that it is working properly when i call a single javascript function through onsubmit and stays on the same page(register).
Problem arises when i call wrapper function which contains 4 other functions, it will show alert messages of all functions without focusing on field when it fails to comply with validation and moves to other page(login page).
Tushar Bhaware
Ranch Hand

Joined: Aug 13, 2012
Posts: 62

Bear Bibeault wrote:The JavaScript question should be handled here. Questions on JSP should be posted in the JSP forum.


i have problem in JavaScript,not in JSP. I thought that i would be better to give relative information about Javascript where it its operating. But anyway I will remember that from next time.
If you can give me references to JSTL and EL would be nice.
Thanks
Tushar Bhaware
Ranch Hand

Joined: Aug 13, 2012
Posts: 62

Eric Pascarello wrote:Also



b can never be null, but you are checking for it in the if.

You are calling submit onclick and onsubmit, no reason for that.

Eric


i will clear onclick event, will retain onsubmit.
thanks
Tushar
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Do you use variable names a,b,c,d and 1,2,3,4,5,6 in your Java code? It is bad practice and I would hate to be the person that has to maintain it in the future.


The reason the form is submitting is you have a JavaScript error. Open up the JavaScript console in your browser of choice and run the code. The error will appear. Depending on your debugger, you may have to persist logs across pages.

Eric
Tushar Bhaware
Ranch Hand

Joined: Aug 13, 2012
Posts: 62

Eric Pascarello wrote:Do you use variable names a,b,c,d and 1,2,3,4,5,6 in your Java code? It is bad practice and I would hate to be the person that has to maintain it in the future.


The reason the form is submitting is you have a JavaScript error. Open up the JavaScript console in your browser of choice and run the code. The error will appear. Depending on your debugger, you may have to persist logs across pages.

Eric


I will remember your advice , will try to give meaningful names from now on.
I will try your idea of checking my javascript in browser console. I have installed firebug.
will let you know what happens.
thanks
tushar
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Did you find your trying to focus on a HTML collection error?

Eric
Tushar Bhaware
Ranch Hand

Joined: Aug 13, 2012
Posts: 62

Considering recommendation from other user, i have adjusted my code to SSCCE form.
My problem remains same. I have tried a lot in two days, changed my validation methods but problem persists.
so here it is the problem- multiple alert message shows when multiple fields remains vacant of my form. what i want is that when multiple field remains vacant,it should show alert message of first vacant field only and not others. Also there is problem with radio button validation function, when radio button is selected, form gets submitted even when other fields are vacant. This doesn't happen with other functions.
i hope you all got my problem.

here it is my code-
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6


  • Ids are singular
    • You have multiple elements with the same id, that is not valid.
    • They can have the same name


  • You can not focus html collections
    • You need to select one of the radio buttons before you focus it

  • You are combining multiple function calls together wrong.
    • Either make a function and call each function
    • or combine them with &&, not a comma




Eric
Tushar Bhaware
Ranch Hand

Joined: Aug 13, 2012
Posts: 62

Thanks for your reply eric ... You have been great help to me.
Eric Pascarello wrote:
Ids are singular
You have multiple elements with the same id, that is not valid.
  • They can have the same name


  • Ok. I will make changes accordingly.

    Eric Pascarello wrote:
    You can not focus html collections.You need to select one of the radio buttons before you focus it
    Eric


    I didn't know that. I will correct it

    Eric Pascarello wrote:
    You are combining multiple function calls together wrong.Either make a function and call each function or combine them with &&, not a comma
    Eric


    I already tried a wrapper function but problem remains the same and when i combined them with && only validate function works which is first one to call , rest don't.
    I googled a lot but couldn't find solution.
    Eric Pascarello
    author
    Rancher

    Joined: Nov 08, 2001
    Posts: 15376
        
        6
    So make a function...



    Eric
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61428
        
      67

    I say just stop trying to be clever and do things in the most straight-worward manner. Nothing is more irritating than code that's unclear, or doesn't work properly, because the author is trying to be clever.

    Tushar Bhaware
    Ranch Hand

    Joined: Aug 13, 2012
    Posts: 62

    Eric Pascarello wrote:So make a function...

    Eric


    It tried it but it still didn't work. There was one problem though "missing variable name" due to comma after myFunc4 instead of semicolon.
    I also tried passing variables (vadiation1 to myFunc2 and so on)as argument to other function but it didn't work either.
    But now i have combined all functions together and made one big function,after that it is working properly which wasn't possible with my earlier validation functions.
    But with your advice,my functions improved (You can see difference with my 1st coding and last coding). Its working now.
    I hope one day i will be able to use wrapper function properly. I will put my working code after this.


    This was my first thread and you helped me immensely, i hope one day i can help someone as you helped me.
    Thank you very much for all your help.

    Tushar
    Tushar Bhaware
    Ranch Hand

    Joined: Aug 13, 2012
    Posts: 62

    Bear Bibeault wrote:I say just stop trying to be clever and do things in the most straight-worward manner. Nothing is more irritating than code that's unclear, or doesn't work properly, because the author is trying to be clever.

    I was not trying to be clever. It's just that i am new to all these programming and i am trying to learn.
    It's just been 4 month since i have taken programming. I hope you got my point. I appreciate you taking time to comment here.

    Tushar.
    Tushar Bhaware
    Ranch Hand

    Joined: Aug 13, 2012
    Posts: 62

    Here is a working code-
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: multiple javascript functions are not working properly