GoodVibe Posted June 1, 2012 Share Posted June 1, 2012 I have the following code: /Sanitize the username var tagBody = '(?:[^"\'>]|"[^"]*"|\'[^\']*\')*'; var tagOrComment = new RegExp( '<(?:' // Comment body. + '!--(??[^->])*--+|-?)' // Special "raw text" elements whose content should be elided. + '|script\\b' + tagBody + '>[\\s\\S]*?</script\\s*' + '|style\\b' + tagBody + '>[\\s\\S]*?</style\\s*' // Regular name + '|/?[a-z]' + tagBody + ')>', 'gi'); function removeTags(html) { var oldHtml; do { oldHtml = html; html = html.replace(tagOrComment, ''); } while (html !== oldHtml); return html.replace(/</g, '<'); } var userNames = new Array("temp","curseword","Temp"); $('#userid').blur(function(){ var userId = $('#userid').val(); var safeId = removeTags(userId); if (safeId.length < 6){ $('#nameCheck').html('The username must be at least 6 characters'); } else if (safeId.length == userId.legth){ $('#nameCheck').html('Please only use alphanumerical characters'); } else if(userId.length >= 6){ var unique = true; for (i=0; i<userNames.length; i++){ if (userId == userNames[i]){ unique = false; } } if (unique == false){ $('#nameCheck').html('The username ' + safeId + ' is taken, please try another one'); } } else { $('#nameCheck').html('The username is correct and you can use it'); } }); Now, the problem I am having is that what i want it to do is that when someone tries to input something that isnt alphanumerical its supposed to tell them so, but so far it does not change the display. Checking for length and such works fine, its just the sanitation part that is giving me troubles. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/263508-forum-validation/ Share on other sites More sharing options...
smoseley Posted June 2, 2012 Share Posted June 2, 2012 Wow, that's an overly complex script. Try this instead: var userNames = new Array("temp","curseword","Temp"); $('#userid').blur(function(){ var userId = $('#userid').val(); var safeId = userId.replace(/[^A-Za-z0-9]+/g, ''); if (safeId.length < 6){ $('#nameCheck').html('The username must be at least 6 characters'); } else if (safeId.length != userId.length){ $('#nameCheck').html('Please only use alphanumerical characters'); } else if(userId.length >= 6){ var unique = true; for (i=0; i<userNames.length; i++){ if (userId == userNames[i]){ unique = false; } } if (unique == false){ $('#nameCheck').html('The username ' + safeId + ' is taken, please try another one'); } } else { $('#nameCheck').html('The username is correct and you can use it'); } }); Quote Link to comment https://forums.phpfreaks.com/topic/263508-forum-validation/#findComment-1350490 Share on other sites More sharing options...
GoodVibe Posted June 2, 2012 Author Share Posted June 2, 2012 Do you mind explaining what that one does? How does it sanitize the data? Quote Link to comment https://forums.phpfreaks.com/topic/263508-forum-validation/#findComment-1350621 Share on other sites More sharing options...
smoseley Posted June 2, 2012 Share Posted June 2, 2012 It only allows alphanumeric characters. Quote Link to comment https://forums.phpfreaks.com/topic/263508-forum-validation/#findComment-1350626 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.