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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

2 js/css questions!!! 1

Status
Not open for further replies.

matrix07

Programmer
Mar 21, 2002
41
CA
hi there. what's wrong with this statement? It's in a function and when the user clicks to submit...I want it to check for a vaild email only if they entered one. My default value is "email". So it shouldn't trigger this statement if the value is "email" or "".

if (document.frmSushiBar.txtEmail.value != "" || document.frmSushiBar.txtEmail.value != "email")
{
if (document.frmSushiBar.txtEmail.value.search("@") == -1 || document.frmSushiBar.txtEmail.value.search("[.*]") == -1)
{
document.frmSushiBar.txterror.value = "Please enter an email";
document.frmSushiBar.txterror.style.display = 'inline';
return false
}
}

**************************************************
*

Next question.

I have a input who's display property is set to "none". And when you click on a button, the property is set to ''. But when I want to clear my form...I have a function which is suppose to set it back to
"none".


<script language=&quot;javascript&quot;>
function reset()
{
document.frmSushiBar.txtNick.value=&quot;nick/handle&quot;
document.frmSushiBar.txtEmail.value=&quot;email&quot;
document.frmSushiBar.txtWebsite.value=&quot;document.frmSushiBar.txtMessage.value=&quot;message&quot;
if (document.frmSushiBar.txterror.style.display = 'inline')
{
document.frmSushiBar.txterror.style.display = 'none';
}
}
</script>


Thanks!!!
 
first question : when do you have problems with this code, always, only when nothing entered (in this case, try
Code:
if .. == null
) ?
second question : you first said &quot;And when you click on a button, the property is set to ''&quot; and then you test
Code:
if (document.frmSushiBar.txterror.style.display = 'inline')
... maybe the error is here ? Water is not bad as long as it stays out human body ;-)
 
Sorry I should have explained it better. Here's all my code.

<script language=&quot;javascript&quot;>
function reset()
{
document.frmSushiBar.txtNick.value=&quot;nick/handle&quot;
document.frmSushiBar.txtEmail.value=&quot;email&quot;
document.frmSushiBar.txtWebsite.value=&quot;document.frmSushiBar.txtMessage.value=&quot;message&quot;
if (document.frmSushiBar.txterror.style.display = 'inline')
{
document.frmSushiBar.txterror.style.display = 'none';
}
}
</script>

<script language=&quot;javascript&quot;>
function validate()
{//start brace
if (document.frmSushiBar.txtNick.value==&quot;&quot; || document.frmSushiBar.txtNick.value==&quot;nick/handle&quot;)
{
document.frmSushiBar.txterror.value = &quot;Please enter a nick/handle&quot;;
document.frmSushiBar.txterror.style.display = 'inline';
return false
}

if (document.frmSushiBar.txtEmail.value != &quot;&quot; || document.frmSushiBar.txtEmail.value != &quot;email&quot;)
{
if (document.frmSushiBar.txtEmail.value.search(&quot;@&quot;) == -1 || document.frmSushiBar.txtEmail.value.search(&quot;[.*]&quot;) == -1)
{
document.frmSushiBar.txterror.value = &quot;Please enter an email&quot;;
document.frmSushiBar.txterror.style.display = 'inline';
return false
}
}

if (document.frmSushiBar.txtMessage.value==&quot;message&quot; || document.frmSushiBar.txtMessage.value==&quot;&quot;)
{
document.frmSushiBar.txterror.value = &quot;Please enter a message&quot;;
document.frmSushiBar.txterror.style.display = 'inline';
return false
}
}//end brace
</script>

So what happens first is when the user clicks on a button which triggers the validate(). It checks to see if all conditions are met before submitting. It works fine but for the email, it still displays the txterror even though I told it to check only if the txtemail textbox is empty or it's content is &quot;email&quot;. It might be the nested if statements because I tried it out with an alert box omitting the nested if statement and it works. For the second question. I have an image which resets the form and it should also set the txterror's display to 'none'.
Because what happens is if anything is wrong with the validation(). The txterror will be displayed with the appropriate value. It's wierd though, cuz as you can see, in the Reset(), I didn't set the txterror.value=&quot;&quot; but when I click on the image the text is gone but the space it occupies is still there? Wierd!!! I hope I made it clear. Wish I could show you but I haven't upload it yet.
Hope you can help me, thanks so much.
 
First : to detect the test that works bad, cut your line in two and add alerts. So replace :
Code:
if (document.frmSushiBar.txtEmail.value.search(&quot;@&quot;) == -1 || document.frmSushiBar.txtEmail.value.search(&quot;[.*]&quot;) == -1)
        {
        ...
        }
by :
Code:
if (document.frmSushiBar.txtEmail.value.search(&quot;@&quot;) == -1) {
   alert (&quot;@ char not found&quot;);
   if (document.frmSushiBar.txtEmail.value.search(&quot;[.*]&quot;) == -1) {
       alert (&quot;regexp not found&quot;);
        ...
    }
}
I'm not enough good in regexp to validate or not your second search. On the other hand, I can advise to replace the first
Code:
document.frmSushiBar.txtEmail.value.Search(&quot;@&quot;)
call by a
Code:
document.frmSushiBar.txtEmail.value.indexOf(&quot;@&quot;)
that seems more apropriate to me in this case.

For the second question, I think your problem is due to the
Code:
display=&quot;inline&quot;
style. I'm not sure, but I think that changing it to
Code:
display=&quot;block&quot;
should solve your problem. Water is not bad as long as it stays out human body ;-)
 
hi Tagol, thanks for your reply. I got the second question working properly but the problem is I'm using a textbox to display my error message. So therefore a user could delete that message. I want to use a regular table cell, but how do I pass that cell the value depending on my validate function? Probably have to use divs right? I'm new to this so if you have a suggestion, I really appreciate that. Thanks again for your reply!!!


Matrix07
 
You can dynamically write anything you want in an HTML page (that's called DHTML ;-)). What you have to do is to identify your cell this way :
Code:
<TD id=&quot;ErrorMsgCell&quot;...>...</TD>
Then, you can add text to your cell using innerHTML property like that (the exemple is in JScript but it works the same way in VBScript):
Code:
var oMsgCell = getElementById(&quot;ErrorMsgCell&quot;);
oMsgCell.innerHtml = &quot;Hey, I just wrote in that cell, That's magic !!!&quot;;

Note that you can directly write html code into the cell as the proporty name is &quot;innerHTML&quot; so you cando this :
Code:
var oMsgCell = getElementById(&quot;ErrorMsgCell&quot;);
oMsgCell.innerHtml = &quot;<B>Hey, I can even write in bold, That's amazing !!!</B>&quot;;
Water is not bad as long as it stays out human body ;-)
 
Hey Targol, yeh I am new to dhtml...but thanks for the reply and solution. I will try that!
 
hey there, I'd tried it...but the text is not showing in my cell. I'd tested with an alert and it's showing there.
I put it in this function which is trigger when the form is submitted. Here's the code.

function check()
{//start brace
var message = document.getElementById(&quot;errormessage&quot;);

if (document.frmSushiBar.txtNick.value==&quot;&quot; || document.frmSushiBar.txtNick.value==&quot;nick/handle&quot;)
{
//document.frmSushiBar.txterror.value = &quot;Please enter a nick/handle&quot;;
//document.frmSushiBar.txterror.style.display = 'inline';
message.innerHtml = &quot;test&quot;;
window.alert(message.innerHtml)
return false
}


and here is my row tags. thanks.

<tr>
<td id=&quot;errormessage&quot; colspan=&quot;3&quot; height=&quot;20&quot;>
<!--<input type=&quot;text&quot; name=&quot;txterror&quot; size=&quot;30&quot; value=&quot;&quot; class=&quot;displayerror&quot;
style=&quot;display:'none'&quot; readonly>-->
</td>
</tr>
 
hey there, I got it working fine now. I just kept it as a text and made it readonly so the user cannot delete or edit the error message. Thanks for all your help!!!
 
you're welcome. Water is not bad as long as it stays out human body ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top