GoodVibe Posted May 22, 2012 Share Posted May 22, 2012 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'); } }); Quote Link to comment https://forums.phpfreaks.com/topic/262923-basic-form-validation-trouble/ Share on other sites More sharing options...
Arnsenal Posted May 24, 2012 Share Posted May 24, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/262923-basic-form-validation-trouble/#findComment-1348202 Share on other sites More sharing options...
GoodVibe Posted May 25, 2012 Author Share Posted May 25, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/262923-basic-form-validation-trouble/#findComment-1348592 Share on other sites More sharing options...
Kays Posted May 25, 2012 Share Posted May 25, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/262923-basic-form-validation-trouble/#findComment-1348601 Share on other sites More sharing options...
haku Posted May 27, 2012 Share Posted May 27, 2012 Is the alert function firing in the code you say isn't working? Try changing this: $(this).attr('value', userId); To this: $(this).val(userId); Quote Link to comment https://forums.phpfreaks.com/topic/262923-basic-form-validation-trouble/#findComment-1348864 Share on other sites More sharing options...
GoodVibe Posted May 29, 2012 Author Share Posted May 29, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/262923-basic-form-validation-trouble/#findComment-1349536 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.