SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast mtran's Avatar
    Join Date
    Apr 2006
    Location
    San Jose, US
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    input text to number validation

    Hi,

    I have 4 input text fileds and want to restrict input values to add to 100.
    Here's my code:
    Code:
    if((parseInt(e.perform_ind.value) + parseInt(e.perform_co.value) + parseInt(e.perform_dept.value) + parseInt(e.perform_combine.value))!== 100){
    alert("Question 9: Values must add to 100.");
    return false;}
    This works if users enter e.g.: 25, 25, 50, 0
    It doesn't work if they didn't enter the last one "0".

    Is there a way to convert "blank" to "0"? So that when a field is left out, the value is implied to equal to 0

    Thanks.

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    var valA = parseInt(.....) || 0;
    which is equivalent to:
    Code:
    var valA = parseInt(...);
    if( isNaN(valA) ) valA = 0;


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •