10

I was making a javascript function in which I need to confirm the input. I wrote the following code but its giving negative value i.e. "else" part even if i enter a valid value. Can some one please suggest a solution?

Html file:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Javascript App</title>
<script type="text/javascript" src="app1.js">
</script>
</head>
<body><h1 id="heading1" style="text-align:center; height:auto; width:auto; font-family:'Arial Black', Gadget, sans-serif">Determinant of a nxn matrix</h1> 
<p id="paragraph1" style="font-family:'Arial Black', Gadget, sans-serif"> This program allows you to compute the determinant of a nxn matrix</p>
<p>
Input the order of the matrix
<br />
<input type="text" maxlength="3" name="value" />
<input type="button" value="submit" onclick="verifyorder(value)" />
</p>
<p id="error"></p>
<p id="detspace"></p>
</body>
</html>

Javascript File:

function verifyorder(order){
;
 if(order>0){
 return true;
 }
 else{
 alert("Sorry, you need to enter a positive integer value, try again");
 document.getElementById('error').innerHTML="Sorry, you need to enter a positive integer value, try again"; 
 }
}
BenMorel
37.1k53 gold badges208 silver badges339 bronze badges
asked Mar 15, 2011 at 17:58
2
  • The code you posted clearly runs after the problems occurs. My guess is that you have passed in a string, not a number, but without the code calling verifyorder I can't tell. Commented Mar 15, 2011 at 18:01
  • There is nothing in your code which associates the (undefined and uninitialised) Javascript variable called "value" with a DOM element which happens to have the name "value". You need in some way to tell the Javascript to find that input element and extract its value. Commented Mar 15, 2011 at 18:03

4 Answers 4

23

Give the textbox an id of "txtValue" and change the input button declaration to the following:

<input type="button" value="submit" onclick="verifyorder(document.getElementById('txtValue').value)" />
answered Mar 15, 2011 at 18:09
Sign up to request clarification or add additional context in comments.

1 Comment

The better answer here, which makes use of the function's arguments.
5

Here is the JSfiddle Demo

I changed your HTML and give your input textfield an id of value. I removed the passed param for your verifyorder function, and instead grab the content of your textfield by using document.getElementById(); then i convert the str into value with +order so you can check if it's greater than zero:

<input type="text" maxlength="3" name="value" id='value' />
<input type="button" value="submit" onclick="verifyorder()" />
</p>
<p id="error"></p>
<p id="detspace"></p> 
function verifyorder() {
 var order = document.getElementById('value').value;
 if (+order > 0) {
 alert(+order);
 return true;
 }
 else {
 alert("Sorry, you need to enter a positive integer value, try again");
 document.getElementById('error').innerHTML = "Sorry, you need to enter a positive integer value, try again";
 }
 }
BenMorel
37.1k53 gold badges208 silver badges339 bronze badges
answered Mar 15, 2011 at 18:04

1 Comment

how if I want to bring param to the javascripts? javascript: somefunc(someparams); like this?
0

Simply put id attribute in your input text field -

<input type="text" maxlength="3" name="value" id="value" />
answered May 31, 2017 at 12:50

Comments

-3

Try: if(parseInt(order)>0){....

answered Mar 15, 2011 at 18:03

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.