Support Forums


Go Back   68 Classifieds Forums > Help & Support > Modules / Plugins / Modifications

Notices

 
LinkBack Thread Tools Display Modes
Old 05-29-2008, 01:40 AM   #1
Senior Member
 
Join Date: Jun 2007
Posts: 110
Rep Power: 11
crystal is on a distinguished road
Smile Mod: Strip bad characters from Username

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}
Replace with this:
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}
Find this (now around line 153):
Code:
<input name="username" type="text" id="username" value="{$username}" size="{$smarty.const.FIELD_SIZE}" />
Replace with this:
Code:
<input name="username" type="text" id="username" value="{$username}" size="{$smarty.const.FIELD_SIZE}" maxlength="30"  onKeyUp="javascript:checkChars(login.username);"/>
Note: I have added a max length of 30 to the username field. This can be changed to your preference. I recommend setting a max length for all fields.
__________________
Crystal
v4.0.8 Dev

Last edited by crystal; 05-29-2008 at 01:44 AM.
crystal is offline   Reply With Quote
Old 05-29-2008, 06:26 AM   #2
cwp
Senior Member
 
cwp's Avatar
 
Join Date: Jun 2007
Posts: 297
Rep Power: 16
cwp is a jewel in the rough
Default

Thanks for sharing!
__________________
Version 4.0.3 Developer
cwp is offline   Reply With Quote
Old 05-29-2008, 06:50 AM   #3
68 Classifieds Staff
 
 
Join Date: Mar 2006
Location: Belmont, NC
Posts: 5,020
Rep Power: 116
Eric Barnes is a jewel in the rough
Default

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
Eric Barnes is offline   Reply With Quote
Old 05-29-2008, 10:46 AM   #4
The Master
 
Maffo's Avatar
 
Join Date: Mar 2006
Location: British in Argentina
Posts: 1,400
Rep Power: 45
Maffo is just really niceMaffo is just really nice
Default

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:
EG [^0-9a-zA-Z
^means NOT then all chars alpha-numeric
__________________
**Bulk Upload System** has been upgraded. Works on all v4 versions with new functionality. Free upgrade for existing customers.
Maffo is offline   Reply With Quote
Old 05-29-2008, 02:19 PM   #5
Moderator
 
juven14's Avatar
 
Join Date: Mar 2006
Location: Texas
Posts: 2,198
Rep Power: 60
juven14 is a jewel in the rough
Default

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:
//used to strip the characters
$username preg_replace('/[^a-z0-9]/i'''$username);

//used to identify if the characters are present
if (preg_match('/[^a-z0-9]/i'$username)) {
    return 
false;//quitting a function
    
die('invalid characters');//quit execution

__________________
John Snyder
PHP Developer
juven14 is offline   Reply With Quote
Old 05-29-2008, 03:55 PM   #6
Senior Member
 
Join Date: Jun 2007
Posts: 110
Rep Power: 11
crystal is on a distinguished road
Default

Quote:
Originally Posted by Maffo View Post
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:
EG [^0-9a-zA-Z
^means NOT then all chars alpha-numeric
Since this isn't an actual part of the validation, I am just using the JavaScript to strip out the characters before they submit. I think this will have a pretty high rate of success, with the exception of the ancient browsers or browers with JavaScript turned off. In this case, those users would have to think of entering different characters. I was hoping username validation would be added into the script by the time too many slipped through.

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
crystal is offline   Reply With Quote
Old 05-29-2008, 04:00 PM   #7
Senior Member
 
Join Date: Jun 2007
Posts: 110
Rep Power: 11
crystal is on a distinguished road
Default

Quote:
Originally Posted by juven14 View Post
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:
//used to strip the characters
$username preg_replace('/[^a-z0-9]/i'''$username);

//used to identify if the characters are present
if (preg_match('/[^a-z0-9]/i'$username)) {
    return 
false;//quitting a function
    
die('invalid characters');//quit execution

John,
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
crystal is offline   Reply With Quote
Old 05-29-2008, 05:14 PM   #8
Moderator
 
juven14's Avatar
 
Join Date: Mar 2006
Location: Texas
Posts: 2,198
Rep Power: 60
juven14 is a jewel in the rough
Default

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:
if ($validate->isError()) 
Add this just above like it is here:

PHP Code:
if (preg_match('/[^a-z0-9]/i'$_POST['username'])) {
  
$errorList $validate->_errorList;
  
$errorList[] = array($_POST['username'], 'Your username has invalid characters.');
  
$validate->_errorList $errorList;
}
if (
$validate->isError()) 
You would have to do this in the file where you modify the user as well.
__________________
John Snyder
PHP Developer
juven14 is offline   Reply With Quote
Old 05-29-2008, 06:34 PM   #9
Senior Member
 
Join Date: Jun 2007
Posts: 110
Rep Power: 11
crystal is on a distinguished road
Default

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.
If we can get this working all the way then there is no need for the javascript (in the developer version). This is what I wanted all along! Is there any way to validate minimum characters in the field also?

Thanks!
__________________
Crystal
v4.0.8 Dev
crystal is offline   Reply With Quote
Old 05-29-2008, 07:12 PM   #10
Moderator
 
juven14's Avatar
 
Join Date: Mar 2006
Location: Texas
Posts: 2,198
Rep Power: 60
juven14 is a jewel in the rough
Default

Change this:

PHP Code:
$errorList[] = array($_POST['username'], 'Your username has invalid characters.'); 
To this:

PHP Code:
$errorList[] = array($_POST['username'], $_POST['username'], 'Your username has invalid characters.'); 
As for the length you can use the built in validation function isLessThan(field_name,error_message,max_integer)

PHP Code:
$validate->isLessThan($_POST['username'], 'The username must contain at least 8 characters/numbers.'8); 
__________________
John Snyder
PHP Developer

Last edited by juven14; 05-29-2008 at 07:20 PM.
juven14 is offline   Reply With Quote

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT -4. The time now is 03:23 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0