Click to See Complete Forum and Search --> : Help


Jick
06-26-2003, 07:28 PM
I need help. The following code has parts in it that will pop-up a alert box if they don't enter a username. I need to add a part to this script that will pop-up a alert that says "You cannot use that as your username" if they try to enter any of the following things in the username box: Administrator, Admin, Admin - Thereusernamehere, thereusernamehere - Admin, also all those things if they enter them in lower case too!

<script type="text/javascript">
<!--
function checkuser(username) {
if (username) {
document.cookie="user="+username+";";
// if ((document.cookie=='style=null') || (!document.cookie)) { //document.cookie="user="+username };
var newPost = document.postform.message.value;
if (!newPost) { alert("There is no message to send!"); return false; }
newPost = newPost.replace(/\r\n\s/g," ");
var chatform = top.msgframe.document.msgfrm
chatform.message.value = newPost;
chatform.username.value = username;
chatform.textFont.value = document.postform.selectFont.value;
chatform.textSize.value = document.postform.selectSize.value;
chatform.textColor.value = document.postform.selectColor.value;
chatform.submit();
document.postform.message.value="";
document.postform.message.focus();
} else { alert('You must enter a username'); }
return false;
}

function getcookie() {
var cookie ='';
if((document.cookie)&&(document.cookie!='style=null')){
username = document.cookie.indexOf("user");
userf = (document.cookie.indexOf("=", username) +1);
userd = document.cookie.indexOf(";", username);
if(userd==-1){userd=document.cookie.length;}
username = document.cookie.substring(userf, userd);
document.postform.username.value = username;
getFont();
getSize();
getClr();
}
}

function getClr(){
var clr = document.postform.selectColor.options;
var clrs = document.cookie.indexOf("fClr");
var cstart = (document.cookie.indexOf("=", clrs) + 1);
var cend = document.cookie.indexOf(";", clrs);
if(cend==-1){cend=document.cookie.length;}
clr = document.cookie.substring(cstart, cend);
document.postform.selectColor.options[clr].selected=true;
}

function setClr(clr){document.cookie="fClr="+clr+";";}

function getFont(){
var font = document.postform.selectFont.options;
var fonts = document.cookie.indexOf("fFace");
var st = (document.cookie.indexOf("=", fonts) + 1)
var nd = document.cookie.indexOf(";", fonts)
if(nd==-1){nd=document.cookie.length;}
font = document.cookie.substring(st, nd);
document.postform.selectFont.options[font].selected=true;
}

function setFont(font){if(font!="x"){document.cookie="fFace="+font+";";} }

function getSize(){
var size=document.postform.selectSize.options;
var sizes = document.cookie.indexOf("fSize");
var sstart = (document.cookie.indexOf("=", sizes) + 1);
var send = document.cookie.indexOf(";", sizes);
if(send==-1){send=document.cookie.length;}
size = document.cookie.substring(sstart, send);
document.postform.selectSize.options[size].selected=true;
}

function setSize(size){document.cookie="fSize="+size+";";}

function check_val(){
if((document.postform.message.value.length>=1)
&&(document.postform.username.value.length>=1||document.postform.username.value!=" ")){
document.postform.elements[1].disabled=false;
} else {document.postform.elements[1].disabled=true; }
}

window.onerror=new Function("return true");
//-->
</script>

Jona
06-26-2003, 07:45 PM
chatform.message.value = newPost;
chatform.username.value = username;
var re = /admin/i;
if(re.exec(username)){alert("You cannot use that name.");
return false;}
chatform.textFont.value = document.postform.selectFont.value;
chatform.textSize.value = document.postform.selectSize.value;
chatform.textColor.value = document.postform.selectColor.value;
chatform.submit();


[Jona]

Charles
06-26-2003, 07:47 PM
<label>User Name<br><input type="text" onchange="if (/\badmin/i.test(this.value)) {alert(this.value + ' is not a valid user name.'); this.value = ''; this.focus}">

Jick
06-26-2003, 08:17 PM
Thanks for your responses. Jona, do you know where in my script I would put that? I don't think at the beggining or the end but where? Thanks alot! :)

Jona
06-26-2003, 08:19 PM
If you look closely at the code, you'll notice that the only added code is in bold.

[Jona]

Jick
06-26-2003, 08:25 PM
Yeah I just noticed that. I'm so dumb some times! :p

How would I make it so it would display the "You cannot use that name" alert and then when the user hits the ok button it would focus their typer thing in the username box?

Charles
06-26-2003, 08:30 PM
Originally posted by mjdimick
How would I make it so it would display the "You cannot use that name" alert and then when the user hits the ok button it would focus their typer thing in the username box? My example above does just that, but it takes a different approach from that of Jona.

Jona
06-26-2003, 08:31 PM
var re = /admin/i;
if(re.exec(username)){alert("You cannot use that name."); document.postform.username.focus();
return false;}


Jona

Jick
06-26-2003, 09:58 PM
I noticed that if you hit the enter key while in the message box on the chat page and hit send it will allow you to send a blank message. I need it so it will not send the message if they press enter and hit send. I want it to say the same message for this as you see when you don't put any message in the message box. Can this be fixed?

Jona
06-26-2003, 10:00 PM
Validate the form onSubmit. Hint: You can use the code I provided as a function.

[Jona]

Jick
06-26-2003, 10:04 PM
Hun? Sorry I didn't quite get what you said! :(

Jona
06-26-2003, 10:08 PM
<script type="text/javascript">
<!--
function validate(f){ if(f.username.value=="" || /\badmin/i.test(f.username.value){ alert("You cannot use that name."); f.username.focus(); return false;} return true;}
//-->
</script>
<!--And your form tag should look like this:-->
<FORM ACTION="action.php" onSubmit="return validate(this);" METHOD="POST">


Also, it would be a good idea to use Charles' code because this is basically the same thing.

[Jona]

Jick
06-28-2003, 04:28 AM
Well looking at the code you posted I don't think you got what I was saying. I was talking about the message box! When you hit the reture key and don't put any message in and hit send it will just show your username and the date but no message. So what I need is the script to just ignore messages with no text in it. If they send a message with no message in it the script won't post it. Also on the user side they will get a message that says you cannot send a blank message. Hope I made better sence that time! Thanks. :D

Jick
06-30-2003, 12:38 AM
Did you understand that? I really need help with this! Thanks. ;)

Jona
06-30-2003, 12:03 PM
Use something like this RegExp:


/^\n$/


[J]ona

Jick
06-30-2003, 02:00 PM
Do you mean something like this:

newPost = newPost.replace(/^\n$/);

If so do I put it in the javascript or the php file? Thanks for your help. :)

Jona
06-30-2003, 02:31 PM
if(/^\n$/.test(newPost)){
//It was found
} else {
//It was not found
}


[J]ona

Jick
06-30-2003, 02:38 PM
And I would put that in my javascript somewhere? Where?

Jona
06-30-2003, 03:02 PM
Originally posted by mjdimick
And I would put that in my javascript somewhere? Where?

Probably right before this line:


chatform.message.value = newPost;


Keep in mind that the code I provided doesn't halt or alert anything. You can add an alert, field.focus();, and/or return false; statement if you like. I would do this:


if(/^\n$/.test(newPost)){
alert("You must enter a message");
return false;}


[J]ona

Jick
06-30-2003, 05:37 PM
I put that in and it still allows you to hit the return key and also I just tryed this and it also allows you to just put a space and send the message. So there are two ways to send a blank message.

1. Hit the return key.
2. Hit the spacebar.

I think what we need to do is make the script only allow messages with actual text in it. If it just has spaces or retures it dosn't send it. ;)

Jona
06-30-2003, 05:42 PM
The code I gave should work fine. I'll work on it and send you the files later.

[J]ona

Jick
06-30-2003, 11:44 PM
Ohh thanks... That will definatly help alot. Thanks. :D

Jick
07-01-2003, 12:53 PM
I will also take answers from any body else. I need help with this as soon as possible. Thanks. ;)

Jona
07-01-2003, 01:12 PM
All right, I did it. Go check the chatroom on my server. (And don't post a link to it from here.)

[J]ona

Jick
07-01-2003, 01:18 PM
Cool it works. Thanks for fixing it. Can you send the updated files to me through e-mail real quick. I'm pretty sure you remember my e-mail address. Thanks so much. Your the best. :D

Jona
07-01-2003, 01:20 PM
Originally posted by mjdimick
I'm pretty sure you remember my e-mail address.

It's in your signature... :rolleyes:

[J]ona

Jick
07-01-2003, 07:32 PM
Thanks. I got it working now. There is one more thing though. I have noticed that if you type a message and hit send it will send the message but once the message box clears after you hit send the send button will still be enabled. Is there something I could add that would disable the send button every time after you send a message. So this is how it would be. Before you send the message its disabled and then you type your message and hit send and then the send button is disabled again. I'm sure its just a simple small peice of code I need to add. :p

Jona
07-01-2003, 07:38 PM
Did you add the onFocus event handler I added to the code? You might want to do a setTimeout("check_val()", 500); or something like that.

[J]ona

Jick
07-01-2003, 07:43 PM
I added all the code you told me to and every thing works just fine. What I'm talking about I also see on yours too. Go to yours or mine and you will see that before you type anything in the message box the send button is disabled right? Then type a message and the send button becomes clickable and then when you hit send it sends the message and the message box clears but the send button is still clickable. I want it so after you type a message and hit send and the message box clears I then want it to re-disable to send button. Hope I clearified that for you. :(

Jona
07-01-2003, 07:48 PM
window.onload = setTimeout("check_val()", "300");


[J]ona

Charles
07-01-2003, 08:40 PM
Jona,

As you are not one of the moderators I can safely take exception to your example above secure in the knowledge that you won't attack my person and then close the thread and instead that if my reply is in error that you will correct me to my benefit. (And I will gladly thank you if you do).

I think that you meant window.onload = function () {setTimeout("check_val()", "300")}

Jona
07-01-2003, 08:44 PM
Originally posted by Charles
As you are not one of the moderators I can safely take exception to your example above secure in the knowledge that you won't attack my person and then close the thread and instead that if my reply is in error that you will correct me to my benefit. (And I will gladly thank you if you do).

All right, so I made a little boo-boo. ;) 'Tis okay, you are right; however, I've already solved the problem with mjdimick over Yahoo Instant Messenger. Thank you for the input, though. :)
And about what you said, it was laughable--not that I'm trying to be mean, but what you said and from past history and reading I've done, it is funny. :D

[J]ona