Jump to content

Recommended Posts

Hi, I have a new problem in that I managed to get my validation working last night, but because it was getting close to 4am, I decided it was time for bed, I woke up a few hours later to work on the script some more, and when I go to my site to see where I left off, I find that the script no longer works, but it did just before I went to bed?

 

Here is the code I typed out:

 

<script type="text/javascript">
$(function(){
var $submit = $("div.submitBtn input");
var $required = $(".required");
function containsBlanks(){
var blanks = $required.map(function(){
return $(this).val() == "";
});
return $.inArray(true,blanks) != -1;
}

function isValidEmail(email){
return email.indexOf("@") != -1;
}

function requiredFields(){
if(containsBlanks()
|| $("#usernameLength input").val().length < 6
|| $("#usernameLength input").val().length > 30/* || !isValidEmail($("#email").val())*/)
$submit.attr("disabled","disabled");
else
$submit.removeAttr("disabled");
}

$("#registerOverlay span").hide();
$("#signIn").click(function(){
$("div.popOverlay").fadeIn("slow");
$("div#registerOverlay").fadeIn("slow");
});
$("div.popOverlay").click(function(){
$(this).fadeOut("slow");
$("div#registerOverlay").fadeOut("slow");
});
$("#registerOverlay input").focus(function(){
$(this).next().fadeIn("slow").css({
background: "pink",
border: "1px solid red"
});
}).blur(function(){
$(this).next().fadeOut("slow");
}).keyup(function(){
// check all required fields
requiredFields();
});

$("#usernameLength input").keyup(function(){
//check string length of username
if($("#usernameLength input").val().length < 6)
$(this).next().removeClass("pass").css({
background: "pink",
border: "1px solid red"
});
else if($("#usernameLength input").val().length > 30)
$(this).next().removeClass("pass").css({
background: "pink",
border: "1px solid red"
});
else
$(this).next().removeClass("error").css({
background: "lightgreen",
border: "1px solid green"
});
});

/*$("#email").keyup(function(){
// check for valid email
if(isValidEmail($(this).val()))
$(this).next().removeClass("error").addClass("pass");
else
$(this).next().removeClass("pass").addClass("error");
});*/

requiredFields();
});
</script>

 

The error that Google Chromes "Inspect Element" is giving me is:

 

"Uncaught TypeError: Cannot read property 'length' of undefined"

 

I've searched google but have not found a solution relevant to my problem.

 

Any help is appreciated :)

Link to comment
https://forums.phpfreaks.com/topic/273064-works-one-moment-but-not-the-next/
Share on other sites

So, you're right.

I've made a simple test:

 

Working

 

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(function(){
	 var value = $("#usernameLength").val().length;
	 console.log(value);

});
</script>
<input type="text" id="usernameLength" value="" />

 

Not working:

 

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(function(){
	 var value = $("#usernameLength input").val().length;
	 console.log(value);

});
</script>
<input type="text" id="usernameLength" value="" />

 

EDIT: If you want to use an input selector, it should be something like this:

http://api.jquery.com/input-selector/

 

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(function(){
	 var value = $(":input#usernameLength").val().length;
	 console.log(value);

});
</script>
<input type="text" id="usernameLength" value="" />

Edited by jazzman1

by implenting the changes and uploading the document then running it on the live site.

 

the site im working on is the Forum Development in my signature, if you go there and "Sign In" at the top of the page, youll notice nothing happens, and both chrome and firebug are complaining about .length being "Undefined"

d'yah what all I want to do is get the jQuery to calculate the minimum and maximum length of the users entered string, if its less than 6 then show an error, if its more than 30 show an error, anything inbetween is acceptable... thats all i wanna do.

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.