Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-23-2013, 02:37 PM   PM User | #1
leonight
New to the CF scene

 
Join Date: Feb 2013
Posts: 5
Thanks: 3
Thanked 0 Times in 0 Posts
leonight is an unknown quantity at this point
javascript validation starter help

help me to find error in the below code

Code:
<html>
<head>
<title></title>

<script>
var user=document.getElementById('abcd');
function validate()
{
if (newa(user))
{
}
return false;
}

function newa(user)
{
var kks=user.value.length
if (kks==0)
{
document.getElementById('ablab').innerHTML="fdfsdfd";
return false;
}
}
</script>


</head>
<body>
<form action="" method="post" onsubmit="return validate()">
<input id="abcd" type="text">
<label id="ablab"></label>
</br>
<input type="submit">
</form>
</body>
</html>
leonight is offline   Reply With Quote
Old 02-23-2013, 03:11 PM   PM User | #2
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,387
Thanks: 18
Thanked 350 Times in 349 Posts
sunfighter is on a distinguished road
What are you trying to do?
sunfighter is offline   Reply With Quote
Old 02-23-2013, 03:17 PM   PM User | #3
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
you define the variable "user" before that element exists, so that will always fail.

either define it within the validate function (which will give it time to get created) or, better, put all of your script after your html, just before the closing /body tag
xelawho is offline   Reply With Quote
Old 02-23-2013, 03:43 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,043
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
You are going a long way round.

var kks=user.value.length
if (kks==0)

First of all, to assign the meaningless name kks to the length of the variable user is plain silly. Always assign meaningful names/ids. Such as uservaluelen. Likewise assigning an id of abcd for the textbox is also plain daft.

But there is no point in having an extra function - all you need is

Code:
<script type = "text/javascript">

function validate() {
document.getElementById('message').innerHTML = "";  // reset
var user = document.getElementById('username').value;
if (user.length) == 0 {
document.getElementById('message').innerHTML = "You must enter  your name";
// re-focus on the input field
return false;
}
}

</script>

Secondly, form validation of the pattern if (document.formname.formfield.value == "") (or length ==0) - that is blank - is barely worthy of the name, and virtually useless, as even a single space, an X or a ? will return false, that is pass the validation. A proper name may only contain letters, hyphen, space and apostrophe.
Numeric values, such as zip codes, phone numbers and dates, should be validated as such. Ditto email addresses. This topic has been covered many times before in this forum.


And Franco di Santo, who never scored here in his spell with Chelsea, threatened to do the same for the opposition. - Commentator BBC 1
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 02-23-2013 at 03:49 PM.. Reason: Missing "
Philip M is offline   Reply With Quote
Old 02-23-2013, 05:09 PM   PM User | #5
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,043
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by xelawho View Post
you define the variable "user" before that element exists, so that will always fail.

either define it within the validate function (which will give it time to get created) or, better, put all of your script after your html, just before the closing /body tag
That won't do! Wherever the script is positioned it will attempt to evaluate

var user=document.getElementById('abcd');

before the user had had any time to enter his details.

You must define the variable user within the function.

But indeed scripts should be positioned in the page right in front of the </body> tag.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 02-24-2013, 08:04 AM   PM User | #6
leonight
New to the CF scene

 
Join Date: Feb 2013
Posts: 5
Thanks: 3
Thanked 0 Times in 0 Posts
leonight is an unknown quantity at this point
i am just practising don't suggest me how to write the code.....

i need your help to find error in above code.the code is not executing
leonight is offline   Reply With Quote
Old 02-24-2013, 10:43 AM   PM User | #7
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,043
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by leonight View Post
i am just practising don't suggest me how to write the code.....

i need your help to find error in above code.the code is not executing
You have been told very clearly why the code is not executing. And it is not a good reason for writing poor code with silly variable names to say that you are "just practising". As a leaner driver do you deliberately or carelessly drive badly when you are "just practising"?

If you want to learn anything and "need help" you should pay attention to the advice of experienced people. However:-

Code:
var rude = true;
var thanks = false;
if (rude && !thanks) {
var interestLost = true;
var moreHelp = 0;
}
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:06 AM.


Advertisement
Log in to turn off these ads.