473,405 Members | 2,373 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,405 software developers and data experts.

validate input field

6 Nibble
Hi, I want to validate the input field 'putime' in the form because even if the 'putime' input box is empty the result is calculated and nightSurcharges is also added to the cost. So I want to validate 'putime' when it is empty.
Expand|Select|Wrap|Line Numbers
  1.  
  2. function TaxiFare() {
  3. // calculates taxi fare based upon miles travelled
  4. // and the hour of the day in military time (0-23).
  5.  
  6. var baseFare = 14;
  7. var costPerMile = 7.00;
  8. var nightSurcharge = 20.50; // 9pm to 6am, every night //its flat 20.50 and not per mile
  9.  
  10. var milesTravelled = Number(document.getElementById("miles").value) || 0;
  11. if ((milesTravelled < 1) || (milesTravelled > 200)) {
  12. alert ("You must enter 1 - 200 miles");
  13. document.getElementById("miles").focus();
  14. return false;
  15. }
  16.  
  17. var pickupTime = Number(document.getElementById("putime").value) || 0;
  18. if ((pickupTime < 0) || (pickupTime > 23) || (pickupTime==null)) {  // note the hours are 0-23.  There is no 24 hour, midnight is 0 hours
  19. alert ("The time must be 0-23 hours");
  20. document.getElementById("putime").focus();
  21. return false;
  22. }
  23.  
  24. var cost = baseFare + (costPerMile * milesTravelled);
  25. // add the nightSurcharge to the cost if it is after
  26. // 8pm or before 6am
  27. if (pickupTime >= 21 || pickupTime < 6) {
  28. cost += nightSurcharge;
  29. }
  30. document.getElementById("result").innerHTML = "Your taxi fare is: $. "  + cost.toFixed(2);
  31. }
  32.  
And here is the form
Expand|Select|Wrap|Line Numbers
  1. <form>
  2. Miles for Journey <input type="text" id = "miles" required><br>
  3. Pickup Time <input type = text id = "putime" required><br><br>
  4. <input type="button" value="Calculate Fare" onclick="TaxiFare()">
  5. <input type="reset" value="Clear"><br><br>
  6. <span id = "result"></span>
  7. </form>
  8.  
Apr 13 '12 #1
11 1957
Expand|Select|Wrap|Line Numbers
  1. if(Textbox1.Text!="")
  2. {
  3. MessageBox.Show("Please Enter The Values");
  4. }
Apr 13 '12 #2
realzahed
6 Nibble
but i need to do it in javascript
Apr 13 '12 #3
Expand|Select|Wrap|Line Numbers
  1. if(document.getelementbyid(textbox id).value!="")
  2. {
  3. alert("Please Enter The Values");
  4. }
  5.  
If This The Corrrect Answer Plz Post Your Result
Apr 13 '12 #4
realzahed
6 Nibble
the script isn't working after i insert the above code even after i press the Calculate Fare button.
I changed the value from textbox id to "putime"
Apr 13 '12 #5
Dont Put the Text id Word ,Instead of give the TextBox id is there in ASP .net give that one.............
Apr 13 '12 #6
realzahed
6 Nibble
sorry but i don't understand what u said.
Apr 13 '12 #7
Give the TextBox id Name...........
Apr 13 '12 #8
realzahed
6 Nibble
no it isn't validating the time field.
Apr 13 '12 #9
acoder
16,027 Expert Mod 8TB
All you need to do is check that putime is not empty.

You can add this condition to line 27, but you will need to get the actual value from the text box as you did in line 17, but without converting to a number.
Apr 13 '12 #10
acoder
16,027 Expert Mod 8TB
Dont Put the Text id Word ,Instead of give the TextBox id is there in ASP .net give that one
There's no indication that the OP is using ASP.NET. In fact, it seems certain that it isn't being used and that this isn't the issue if you read the question again.
Apr 13 '12 #11
realzahed
6 Nibble
ok i got it by adding (pickupTime == "") to line 27. Thank you acoder!
Apr 13 '12 #12

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

Similar topics

3
by: Ben | last post by:
Hi all, i have file input field in a form like this: <input name="isbn" type="text" size="25" value="<?php echo $_POST ?>" /> <input name="image" type="file" size="25" value="<?php echo...
13
by: Eddie | last post by:
I need to validate a text input field. I just want to say if user enters 93101 or 93102 or 93103 or 93105 or 93106 or 93107 or 93108 or 93109 or 93110 or 93111 or 93116 or 93117 or 93118 or...
1
by: mats | last post by:
Hell My problem is that I do not know how to validate input that I let the user type into my datagrid In one grid I let the user to type in a value into a template column - how do I make sure...
0
by: suresh | last post by:
How to validate input with datagrid control? i tried the following code from msdn...i have problem in adding handler to the columnchanging event..its not working for me? any bug in this code? is...
13
by: Lee | last post by:
I have this function that doesn't work. I pass it the td element and an id, and it makes an input field inside the td. That part workds. What doesn't work is that I want to add an "onkeyup" on...
1
by: SkipNRun | last post by:
I am a novice when comes to JavaScript, AJAX. I am working on a form, which will allow users to update their contact information. In order to make the form flexible, I need to use pull down list. ...
10
mariodavi
by: mariodavi | last post by:
Hi everybody, how to validate input text in the follow: if option combo = 1,2 e 3, input 1 and input 2 must be filled, if one of this values not selected in combo (to be others), filled input...
8
idsanjeev
by: idsanjeev | last post by:
I try to validate inpute field for weight that accept only numeric value like 65.12 not allow any special character or character but allow '.' . thanks jha
1
by: kasturi207 | last post by:
I want to validate input (text) which i have to check with databse record is exist. the code something like that:- <cfscript> function checkkp() { if (rscheck.recordcount NEQ 0) {...
5
by: omar999 | last post by:
I have a CMS which displays some flight routes, alongside prices, dates which is using asp and sql server 05. works well - i.e an update on the CMS page populates the sql table and then the asp...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.