473,383 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

validating a number entered in a textfield

11
Hi friends,
My form have a text field named "price" which should have only number and if it contains decimal number then it should not have more than two numbers after decimal. I tried this but it is not working . If the data entered is valid (as described above) then it should post to the target.php .....

I hope u will point out my mistake OR u may advice some wise alternative for this ..
thanks.....
The code goes as follows.....

[HTML]<html>
<head>
<script type="text/javascript">
function checkForm()
{
// var price = document.form1.price;
document.form1.submit();

var value=document.getElementById('price').value;
var indx=value.indexOf('.');
var digit=value.substring(indx+1);

if(digit.length>2)
{
alert('only two decimal digit allowed');
document.form1.price.focus();
price.select();
document.form1.price.focus();
return false;
}
else
{
document.form1.submit();

}
}
</script>
</head>
<body>
<form name="form1" id="price" method="post" action="print selected area.html" >

<input name="price" type="text" id="price" size="25" maxlength="16" />

<input type="button" name="submit" value="ADD" onsubmit="chkForm()"/>
</form>
</body>
</html>
[/HTML]
Dec 27 '07 #1
2 1632
foss
11
Sorry to disturb u friends .
I got the solution to the problem . I has made some silly mistakes
the solution is

[HTML]<html>
<head>
<script type="text/javascript">


function check_price()
{
// var price = document.form1.price;
var value=document.getElementById('price').value;
var indx=value.indexOf('.');
var digit=value.substring(indx+1);

if (isNaN(value)== true )
{
alert("Please enter a valid number");
document.form1.price.focus();
price.select();
document.form1.price.focus();
return false;
}

else if(digit.length>2)
{
alert('Only two decimal digits allowed');
document.form1.price.focus();
price.select();
document.form1.price.focus();
return false;
}
else
{
document.form1.submit();
}


}



</script>
</head>
<body>
<form name="form1" method="post" action="control_center.php" >

<input name="price" type="text" id="price" size="25" maxlength="16" />

<input type="button" name="submitButton" value="ADD" onFocus="check_price()"/>
</form>
</body>
</html>[/HTML]
Dec 27 '07 #2
acoder
16,027 Expert Mod 8TB
Sorry to disturb u friends .
I got the solution to the problem . I has made some silly mistakes
the solution is
price.select() would result in an error because the line defining price at the beginning of the function has been commented out.
Dec 27 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Mats | last post by:
It's good practice to validate input, not only where it should be coming from, but from anywhere it's possible to change or add input for a "client". If all user input is transfered using "post"...
3
by: Mark | last post by:
Hi, Im trying to validate a form, all the validating works apart from one field. This particular field must consist of the first 2 characters as letters, & the following 5 as numbers. And if it...
1
by: platostoteles | last post by:
Hallo NG, I am new to JavaScript and would really appreciate any help to solve my problem. I am using the blow code in my form to validate form fields. What I would like to accomplish is that...
9
by: chuck | last post by:
I need some help with validating user input. I am writing a C computer program for an intro to C course. Here is the situation. I am creating an application that will do currency conversions. ...
3
by: ASP Developer | last post by:
I have a generic list of objects. These objects have a text field that contains a letter for the first character and numbers for the next three. For example, "A001". I need to sort these by...
5
by: Kavya | last post by:
I saw these two ways for validating input First Way -------------- #include <iostream> #include <limits> using namespace std; int main() {
16
by: shyamg | last post by:
Hi, this is my javascript validating the fields in mozilla FF but its working and validating only one field. how to write the and how to works the script .................. function...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.