Jump to content

forum validation


GoodVibe

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/263508-forum-validation/
Share on other sites

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');
            }
});

Link to comment
https://forums.phpfreaks.com/topic/263508-forum-validation/#findComment-1350490
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.