Jump to content

Recommended Posts

I am trying to make a basic form for registering, and trying to validate it. For some reason, it will not call the js at all. I have even reated an alert to popup when the page loads and i am getting nothing. Maybe Im missing something simple, so any help would be appreciated.

 

Here is my main file:

<html lang="en">
    <head>
        
    </head>
    <body>
        <form action="#">
            <b>User Name</b> <input type="text" id="userid" />
            <span id="nameCheck"></span>
            <br/><br/>
            <b>Password</b> <input type="password" id="password" />
            <span id="passCheck"></span>
            <br/><br/>
            <b>Re-enter Password</b><input type="password" id="password2" />
            <span id="pass2Check"></span>
            <br/><br/>
            <b>E-mail</b><input type="text" id="email" />
            <span id="mailCheck"></span>
            <br/><br/>
            <b>Re-enter Email</b><input type="text" id="email2" />
            <span id="mail2Check"></span>
        </form>
        
        <script type='text/javascript' src="js/jquery.js"></script>
        <script type='text/javascript' src="js/register.js"></script>
    </body>
</html>

 

And here is my js:

 

$(function(){
    alert ('We have JS');
});

var userNames = new Array("temp","curseword","Temp");

$('#userid').blur(function(){
    var userId = $(this).val();
    alert (userId);
    userId = userId.replace(/<[^>]*>?/g, '');
    $(this).attr('value', userId);
    if (userId.length < 6){
         $('#nameCheck').html('The username must be at least 6 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 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/262923-basic-form-validation-trouble/
Share on other sites

Are you using  Firefox's Fire bug or Chrome's equivalent? Using those tools -- If you look under the console tab while reloading your page you may find errors that help you.

 

Are your file paths correct when linking your JS files... I.E.  you may be missing ../

 

<script type='text/javascript' src="../js/jquery.js"></script>

<script type='text/javascript' src="../js/register.js"></script>

Hi, no, I haven't been using the addons, completely forgot about them.

 

As for the file paths, I know that the ../ means to go one folder up in the tree correct?

 

The file paths are correct, and after a little bit more searching I have found out that my problem lies in:

 

var userId = $(this).val();
    alert (userId);
    userId = userId.replace(/<[^>]*>?/g, '');
    $(this).attr('value', userId);

 

I was trying to use this to sanitize my input, but it seems to be crashing the program. ANy suggestions on that?

 

Also, I had a quick question. I have seen people add the line

 

<script type='text/javascript' src="js/register.js"></script>

 

In the header of the page. The tutorials I have been following tell me to do so at the end of the body. What is the difference?

 

Thanks

If you could tell that, could you also narrow it down to 1 line? :)

 

Libraries should be loaded in head. All else in end of body. The reason is that if you stuff all your scripts at the top, it can slow down page load times. Since the browser has to parse all of them before it gets to the important HTML that you want the user to see. So placing them at the end helps that. But libraries should to be at the top because you may want to call them anywhere within the page.

Ok, so i narrowed it down to either one of these lines:

 

userId = userId.replace(/<[^>]*>?/g, '');
$(this).attr('value', userId);

 

As to the change:

 

 

$(this).attr('value', userId);

 

to

$(this).val(userId);

 

will that also change the value? I am trying to make it so that when the user types a character that is not valid it removes and keeps the valid username.

 

Thanks for the help so far

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.