Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with JS to JQ 1

Status
Not open for further replies.

litton1

Technical User
Apr 21, 2005
584
GB
Hi all,

I have a JavaScript function that checks values have been added to textboxes on button click. If no values have been added then the backgrounds are changed to red, a message is added and false is returned. If they have then true is returned. I am having problems getting the correct JQuery syntax function. At the moment i do this...Any help appreciated.

Code:
function valSubmit() {
    var doc = document.forms[0];
    var msg = "";
    if (doc.TxtYourName.value == "") {
       msg += "- Please enter your name";
       document.getElementById('TxtYourName').style.background = '#FBBBB9';
    } else {
       document.getElementById('TxtYourName').style.background = '#fff';
    }
   ....
    if (msg == "") {
       var tempLabel = document.getElementById('lblerrormessage');
       tempLabel.innerText = "Please wait, sending....";
       return true;
    }
    else {
        return false;
     }
}

Thanks,
L

Age is a consequence of experience
 
this should work
Code:
[b][COLOR=#0000FF]function[/color][/b] [b][COLOR=#000000]valSubmit[/color][/b][COLOR=#990000]()[/color] [COLOR=#FF0000]{[/color]
[tab][b][COLOR=#0000FF]var[/color][/b] msg [COLOR=#990000]=[/color] [COLOR=#FF0000]""[/color][COLOR=#990000];[/color]
[tab][b][COLOR=#0000FF]var[/color][/b] ctl [COLOR=#990000]=[/color] $[COLOR=#990000]([/color][COLOR=#FF0000]'#TxtYourName'[/color][COLOR=#990000]);[/color]
[tab][b][COLOR=#0000FF]if[/color][/b] [COLOR=#990000]([/color]ctl[COLOR=#990000].[/color][b][COLOR=#000000]val[/color][/b][COLOR=#990000]()[/color] [COLOR=#990000]==[/color] [COLOR=#FF0000]""[/color][COLOR=#990000])[/color][COLOR=#FF0000]{[/color]
[tab]   msg [COLOR=#990000]+=[/color] [COLOR=#FF0000]"- Please enter your name"[/color][COLOR=#990000];[/color]
[tab]   ctl[COLOR=#990000].[/color][b][COLOR=#000000]css[/color][/b][COLOR=#990000]([/color][COLOR=#FF0000]'background-color'[/color][COLOR=#990000],[/color] [COLOR=#FF0000]'#FBBBB9'[/color][COLOR=#990000]);[/color]
[tab][COLOR=#FF0000]}[/color] [b][COLOR=#0000FF]else[/color][/b] [COLOR=#FF0000]{[/color]
[tab]   ctl[COLOR=#990000].[/color][b][COLOR=#000000]css[/color][/b][COLOR=#990000]([/color][COLOR=#FF0000]'background-color'[/color][COLOR=#990000],[/color] [COLOR=#FF0000]'#fff'[/color][COLOR=#990000]);[/color]
[tab][COLOR=#FF0000]}[/color]
[tab][b][COLOR=#0000FF]if[/color][/b] [COLOR=#990000]([/color]msg [COLOR=#990000]==[/color] [COLOR=#FF0000]""[/color][COLOR=#990000])[/color] [COLOR=#FF0000]{[/color]
[tab]   $[COLOR=#990000]([/color][COLOR=#FF0000]'#lblerrormessage'[/color][COLOR=#990000]).[/color][b][COLOR=#000000]text[/color][/b][COLOR=#990000]([/color][COLOR=#FF0000]"Please wait, sending...."[/color][COLOR=#990000]);[/color]
[tab]   [b][COLOR=#0000FF]return[/color][/b] [b][COLOR=#0000FF]true[/color][/b][COLOR=#990000];[/color]
[tab][COLOR=#FF0000]}[/color]
[tab][b][COLOR=#0000FF]else[/color][/b] [COLOR=#FF0000]{[/color]
[tab][tab][b][COLOR=#0000FF]return[/color][/b] [b][COLOR=#0000FF]false[/color][/b][COLOR=#990000];[/color]
[tab][COLOR=#FF0000]}[/color]
[COLOR=#FF0000]}[/color]
 
Thanks for your time.

I will give this a go when I get chance to get on a machine. I will let you know how I get on :)

Age is a consequence of experience
 
Just an observation: you are not doing anything with the MSG string after the function ends. You don't put it in the Dom nor return it. So it is dead to the global scope.

Also it looks like you are doing this for form validation. Jquery has a number of excellent validation plugins that might make life simpler for you. Remember that you can't rely on JavaScript validation though. Always revalidate server side.
 
Thanks for that. You are right there is a jquery plugin on there site ( ) I will have go with that. It is all checked on the server so no probs there. Just trying to better my client side though...

Have a star for your trouble.

Age is a consequence of experience
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top