|
|
#1 |
|
Senior Member
Join Date: Jun 2007
Posts: 110
Rep Power: 11 ![]() |
I did this so my site's usernames would not be email or web addresses or contain any special characters. This mod will strip out all bad characters while the user is typing. It will allow only A-Z, 0-9, and underscore (_) to be typed into the username field.
Open templates/default/user/userjoin.tpl Find this (lines 1-28) Code:
{literal}
<script language="JavaScript" type="text/javascript">
<!-- //
function checkform(frm) {
{/literal}
{$validation}
{literal}
}
function showterms(){
newwin=window.open("terms.php","Terms","menubar=no, scrollbars=yes, width=420, height=380, directories=no,location=no,resizable=yes,status=no,toolbar=no");
}
function new_freecap()
{
// loads new freecap image
if(document.getElementById)
{
// extract image name from image source (i.e. cut off ?randomness)
thesrc = document.getElementById("freecap").src;
thesrc = thesrc.substring(0,thesrc.lastIndexOf(".")+4);
// add ?(random) to prevent caching
document.getElementById("freecap").src = thesrc+"?"+Math.round(Math.random()*100000);
} else {
alert("Sorry, cannot autoreload freecap image\nSubmit the form and a new freecap will be loaded");
}
}
//-->
</script>
{/literal}
Code:
{literal}
<script language="JavaScript" type="text/javascript">
<!-- //
function checkform(frm) {
{/literal}
{$validation}
{literal}
}
function showterms(){
newwin=window.open("terms.php","Terms","menubar=no, scrollbars=yes, width=420, height=380, directories=no,location=no,resizable=yes,status=no,toolbar=no");
}
function new_freecap()
{
// loads new freecap image
if(document.getElementById)
{
// extract image name from image source (i.e. cut off ?randomness)
thesrc = document.getElementById("freecap").src;
thesrc = thesrc.substring(0,thesrc.lastIndexOf(".")+4);
// add ?(random) to prevent caching
document.getElementById("freecap").src = thesrc+"?"+Math.round(Math.random()*100000);
} else {
alert("Sorry, cannot autoreload freecap image\nSubmit the form and a new freecap will be loaded");
}
}
//Strip bad characters
var badChars = /[-?!$\\@\/\#%\^\&\*\(\)\[\]\+\{\}\`\~\=\.,\'\";:|><]/;
function checkChars(val) {
var strPass = val.value;
var strLength = strPass.length;
var lchar = val.value.charAt((strLength) - 1);
if(lchar.search(badChars) != -1) {
var tst = val.value.substring(0, (strLength) - 1);
val.value = tst;
}
}
// -->
</script>
{/literal}
Code:
<input name="username" type="text" id="username" value="{$username}" size="{$smarty.const.FIELD_SIZE}" />
Code:
<input name="username" type="text" id="username" value="{$username}" size="{$smarty.const.FIELD_SIZE}" maxlength="30" onKeyUp="javascript:checkChars(login.username);"/>
__________________
Crystal v4.0.8 Dev Last edited by crystal; 05-29-2008 at 01:44 AM. |
|
|
|
|
|
#2 |
|
Senior Member
Join Date: Jun 2007
Posts: 297
Rep Power: 16 ![]() |
Thanks for sharing!
__________________
Version 4.0.3 Developer |
|
|
|
|
|
#3 |
|
68 Classifieds Staff
Join Date: Mar 2006
Location: Belmont, NC
Posts: 5,020
Rep Power: 116 ![]() |
Yes very nice.
__________________
Eric Barnes 68 Classifieds Developer Please do not send me a private message asking for support. Instead use these open forums or our ticket system. Customer Area | Issue Tracker | Documentation | 68C Mods | Submit a Ticket | 68 @ Twitter | My Modules |
|
|
|
|
|
#4 |
|
The Master
Join Date: Mar 2006
Location: British in Argentina
Posts: 1,400
Rep Power: 45 ![]() ![]() |
Remember you will need to do this at php level also, javascript should only be used as a pre-validation and never relied on.
Also in my opinion it would be logical to have the characters you can have, rather than the ones you cant as there are literally hundreds in different languages. PHP Code:
__________________
**Bulk Upload System** has been upgraded. Works on all v4 versions with new functionality. Free upgrade for existing customers. |
|
|
|
|
|
#5 |
|
Moderator
|
I agree with Maffo, all one has to do is turn off javascript and then they can bypass this process altogether. I think it is a worthy contribution, just be sure that your code is processed on the php end as well.
PHP Code:
__________________
John Snyder PHP Developer |
|
|
|
|
|
#6 | |
|
Senior Member
Join Date: Jun 2007
Posts: 110
Rep Power: 11 ![]() |
Quote:
If there is a way to include this in the php validation instead, I would much rather have that code! I did this to eliminate the problem the only way I could. I am hoping to add minimum characters to that field also. I think both things are essential in validation and should be added in to the script. I'm not worried about other language characters because my site is in English, but I would like to clean up the code. I want do something like the php [^0-9a-zA-Z] but I haven't gotten the javascript to work yet in the stripping the field method, so the code I have works for now. Please feel free to clean it up!
__________________
Crystal v4.0.8 Dev |
|
|
|
|
|
|
#7 | |
|
Senior Member
Join Date: Jun 2007
Posts: 110
Rep Power: 11 ![]() |
Quote:
Thanks for the reply. Where would I put this PHP code? Would this include checking the username in server side validation (i.e. return an error message if the characters are present)? Or would it just strip the characters before entering into the database? I was hoping to get this like the other fields and have it checked on submit and return an alert box if neccessary.
__________________
Crystal v4.0.8 Dev |
|
|
|
|
|
|
#8 |
|
Moderator
|
You could do this with JavaScript to return the alert box, however, with PHP you would have to submit the form for it to then be processed and validated server-side.
I don't have a functional install of v4 to test this on, but I think it might work. Open userjoin.php find (around line:153): PHP Code:
PHP Code:
__________________
John Snyder PHP Developer |
|
|
|
|
|
#9 |
|
Senior Member
Join Date: Jun 2007
Posts: 110
Rep Power: 11 ![]() |
John,
I tried the code you just posted and it works, except the error message. It will stop the form from being submitted if there are any special characters in the username field but the error message appears as: Code:
The operation could not be performed because one or more error(s) occurred. Please resubmit the form after making the following changes: 1. Is there any way to validate minimum characters in the field also? Thanks!
__________________
Crystal v4.0.8 Dev |
|
|
|
|
|
#10 |
|
Moderator
|
Change this:
PHP Code:
PHP Code:
PHP Code:
__________________
John Snyder PHP Developer Last edited by juven14; 05-29-2008 at 07:20 PM. |
|
|
|
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| no username? | outthere | v4 Questions & Support | 0 | 11-08-2007 08:41 PM |
| Using "modify ad" overrides max characters setting | Avi8r | v3.1 Questions & Support | 3 | 08-18-2007 01:32 PM |
| How To Scrub & Replace Characters In Input Fields? | bgordon | v3.1 Questions & Support | 5 | 04-15-2007 05:49 PM |
| Number of Characters in Title and Desc | pbraz | v3.1 Questions & Support | 1 | 02-06-2007 08:50 AM |
| Retrieving a lost username | spaceboy | v3.1 Questions & Support | 1 | 09-09-2006 05:42 PM |