Regular Expression for signed floating point number not working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hello2008
    New Member
    • Dec 2007
    • 14

    Regular Expression for signed floating point number not working

    Hi,

    I donot have much knowledge of R.Exp. In my web page I am required to validate numeric textboxes allowing signed/unsigned integer/float numbers and then compare between them. I need the textboxes to accept values only between -10 to +10. Please help? Below is my code

    [PHP]
    <html>
    <head>
    <title>
    Display Requirements
    </title>
    <script language = "javascript ">

    var numericExpressi on = /^(((-|\+)?\d+(\.\d*) ?)|((-|\+)?(\d*\.)?\d +))$/ ; // this exp should accept both signed/unsigned integer/float numbers

    for(var i = 1; i <= 5; i++)
    {
    //alert("in for");

    var FL_id = 'FL' + i;
    var TL_id = 'TL' + i;
    var FH_id = 'FH' + i;
    var TH_id = 'TH' + i;



    var FL_value = document.getEle mentById(FL_id) .value;

    var TL_value = document.getEle mentById(TL_id) .value;


    var FH_value = document.getEle mentById(FH_id) .value;


    var TH_value = document.getEle mentById(TH_id) .value;


    // ---------- From LOW validation(can' t be blank or non-numeric) -----------
    if(FL_value == "" || FL_value == null)
    {
    alert("The Ranges cannot be left blank") ;
    document.getEle mentById(FL_id) .style.backgrou nd = 'Yellow';
    document.getEle mentById(FL_id) .focus();
    return false;

    }//if(FL_value == "" || FL_value == null) over
    else
    {
    var float_FL_value = parseFloat(FL_v alue) ;
    //alert("float_FL _value: " + float_FL_value) ;

    if(FL_value.mat ch(numericExpre ssion))
    {
    document.getEle mentById(FL_id) .style.backgrou nd = 'White';
    }// if(FL_value.mat ch(numericExpre ssion)) over
    else
    {
    alert("The Ranges can only be numeric");
    document.getEle mentById(FL_id) .style.backgrou nd = 'Yellow';
    document.getEle mentById(FL_id) .value = "" ;
    document.getEle mentById(FL_id) .focus() ;
    return false;
    }// else(FL_value.m atch(numericExp ression)) over

    }// else (FL_value.match (numericExpress ion)) over


    // ---------- To LOW validation(can' t be blank or non-numeric) -----------


    if(TL_value == "" || TL_value == null)
    {
    alert("The ranges cannot be left blank") ;
    document.getEle mentById(TL_id) .style.backgrou nd = 'Yellow';
    document.getEle mentById(TL_id) .focus();
    return false;
    }//if(TL_value == "" || TL_value == null) over
    else
    {
    var float_TL_value = parseFloat(TL_v alue);
    //alert("float_TL _value: " + float_TL_value) ;

    if(TL_value.mat ch(numericExpre ssion))
    {
    document.getEle mentById(TL_id) .style.backgrou nd = 'White';
    }// if(TL_value.mat ch(numericExpre ssion)) over
    else
    {
    alert("The Ranges can only be numeric");
    document.getEle mentById(TL_id) .style.backgrou nd = 'Yellow';
    document.getEle mentById(TL_id) .value = "" ;
    document.getEle mentById(TL_id) .focus() ;
    return false;
    }// else(TL_value.m atch(numericExp ression)) over

    }// else (TL_value.match (numericExpress ion)) over


    // ---------- From HIGH validation(can' t be blank or non-numeric) -----------
    if(FH_value == "" || FH_value == null)
    {
    alert("The ranges cannot be left blank") ;
    document.getEle mentById(FH_id) .style.backgrou nd = 'Yellow';
    document.getEle mentById(FH_id) .focus();
    return false;
    }//if(FH_value == "" || FH_value == null) over
    else
    {
    var float_FH_value = parseFloat(FH_v alue);
    if(FH_value.mat ch(numericExpre ssion))
    {
    document.getEle mentById(FH_id) .style.backgrou nd = 'White';
    }// if(FH_value.mat ch(numericExpre ssion)) over
    else
    {
    alert("The Ranges can only be numeric");
    document.getEle mentById(FH_id) .style.backgrou nd = 'Yellow';
    document.getEle mentById(FH_id) .value = "" ;
    document.getEle mentById(FH_id) .focus() ;
    return false;
    }// else(FH_value.m atch(numericExp ression)) over

    }// else (FH_value.match (numericExpress ion)) over


    // ---------- To HIGH validation(can' t be blank or non-numeric) -----------
    if(TH_value == "" || TH_value == null)
    {
    alert("The ranges cannot be left blank") ;
    document.getEle mentById(TH_id) .style.backgrou nd = 'Yellow';
    document.getEle mentById(TH_id) .focus();
    return false;
    }//if(TH_value == "" || TH_value == null) over
    else
    {
    var float_TH_value = parseFloat(TH_v alue);
    if(TH_value.mat ch(numericExpre ssion))
    {
    document.getEle mentById(TH_id) .style.backgrou nd = 'White';
    }// if(TH_value.mat ch(numericExpre ssion)) over
    else
    {
    alert("The Ranges can only be numeric");
    document.getEle mentById(TH_id) .style.backgrou nd = 'Yellow';
    document.getEle mentById(TH_id) .value = "" ;
    document.getEle mentById(TH_id) .focus() ;
    return false;
    }// else(TH_value.m atch(numericExp ression)) over

    }// else (TH_value.match (numericExpress ion)) over

    // Validation for checking that To-Low is greater than From-Low; From-High is greater than To-Low; To-High is greater than From-High

    //alert("here");
    if(!(float_TL_v alue > float_FL_value) )// means if to-low's value is NOT greater than from-low's value, then error-alert
    {
    alert("TO-LOW has to be greater than FROM-LOW");
    document.getEle mentById(TL_id) .style.backgrou nd = 'Yellow';
    document.getEle mentById(TL_id) .focus();
    return false;
    }

    if(!(float_FH_v alue > float_TL_value) )// means if from-high's value is NOT greater than to-low's value, then error-alert
    {
    alert("FROM-HIGH has to be greater than TO-LOW");
    document.getEle mentById(FH_id) .style.backgrou nd = 'Yellow';
    document.getEle mentById(FH_id) .focus();
    return false;
    }

    if(!(float_TH_v alue > float_FH_value) )// means if to-high's value is NOT greater than from-high's value, then error-alert
    {
    alert("TO-HIGH has to be greater than FROM-HIGH");
    document.getEle mentById(TH_id) .style.backgrou nd = 'Yellow';
    document.getEle mentById(TH_id) .focus();
    return false;
    }

    } //for(var i = 1; i <= 5; i++) over


    return true;
    }// checkForm() over
    </script>
    </HEAD>
    <BODY onLoad = "bodyLoad() ">
    <form name = "DispForm" method = "get" action = "InsertJobRepMa ster.php" onsubmit = "return checkForm()">

    <fieldset>
    <legend align = "center">
    <b><font color = "blue">Disp lay Requirements </font></b>
    </legend>
    <b>Company Name: <?php echo '<font color = "red">' . $_SESSION['companyname'] . '</font>' ;?></b><br>
    <b>Job Name: <?php echo '<font color = "red">' . $_SESSION['jobname'] . '</font>' ;?></b><br>
    <p align ="right">
    <b>Welcome: <?php echo '<font color = "red">' . $_SESSION['username'] . '</font>' ; ?> </b> &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;
    </p>
    <br>
    <br>
    <br>
    <table id = "T1" align = "center" border = "1" cellpadding = "3" cellspacing = "0" style = "border-collapse: collapse" bordercolor = "#COCOCO">

    <tr bgcolor = "pink">
    <td>Sr. No</td>
    <td>Qualities </td>
    <td colspan = "2" align = "center">Lo w </td>
    <td colspan = "2" align = "center">Hi gh </td>
    </tr>

    <tr>
    <td></td>
    <td></td>
    <td>From </td>
    <td>To </td>
    <td>From </td>
    <td>To </td>
    </tr>

    <tr>
    <td bgcolor = "orange">1. </td>
    <td>Exact Job-Individual Match </td>
    <td><input tabindex = "1" type = "text" size = "5" id = "FL1" name = "FL1"></td>
    <td><input tabindex = "2" type = "text" size = "5" id = "TL1" name = "TL1"></td>
    <td><input tabindex = "3" type = "text" size = "5" id = "FH1" name = "FH1"></td>
    <td><input tabindex = "4" type = "text" size = "5" id = "TH1" name = "TH1"></td>
    </tr>

    <tr>
    <td bgcolor = "orange">2. </td>
    <td>Good Compatibility to Job Requirement </td>
    <td><input tabindex = "5" type = "text" size = "5" id = "FL2" name = "FL2"></td>
    <td><input tabindex = "6" type = "text" size = "5" id = "TL2" name = "TL2"></td>
    <td><input tabindex = "7" type = "text" size = "5" id = "FH2" name = "FH2"></td>
    <td><input tabindex = "8" type = "text" size = "5" id = "TH2" name = "TH2"></td>
    </tr>

    <tr>
    <td bgcolor = "orange">3. </td>
    <td>Fair Compatibility to Job Requirement </td>
    <td><input tabindex = "9" type = "text" size = "5" id = "FL3" name = "FL3"></td>
    <td><input tabindex = "10" type = "text" size = "5" id = "TL3" name = "TL3"></td>
    <td><input tabindex = "11" type = "text" size = "5" id = "FH3" name = "FH3"></td>
    <td><input tabindex = "12" type = "text" size = "5" id = "TH3" name = "TH3"></td>
    </tr>

    <tr>
    <td bgcolor = "orange">4. </td>
    <td>Poor Compatibility to Job Requirement </td>
    <td><input tabindex = "13" type = "text" size = "5" id = "FL4" name = "FL4"></td>
    <td><input tabindex = "14" type = "text" size = "5" id = "TL4" name = "TL4"></td>
    <td><input tabindex = "15" type = "text" size = "5" id = "FH4" name = "FH4"></td>
    <td><input tabindex = "16" type = "text" size = "5" id = "TH4" name = "TH4"></td>
    </tr>

    <tr>
    <td bgcolor = "orange">5. </td>
    <td>Over-Focused Compatibility to Job Requirement </td>
    <td><input tabindex = "17" type = "text" size = "5" id = "FL5" name = "FL5"></td>
    <td><input tabindex = "18" type = "text" size = "5" id = "TL5" name = "TL5"></td>
    <td><input tabindex = "19" type = "text" size = "5" id = "FH5" name = "FH5"></td>
    <td><input tabindex = "20" type = "text" size = "5" id = "TH5" name = "TH5"></td>
    </tr>

    <tr>
    <td colspan = "2" align = "center" ><input tabindex = "21" type = "submit" name= "ok" value = " OK "></td>
    <td colspan = "4" align = "center" ><input tabindex = "22" type = "button" name = "cancel" value = " Cancel "> </td>
    </tr>

    </table>
    </head>
    </form>
    </body>
    </html>
    [/PHP]
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    hi ...

    instead of using a regExp you could use a function like the following:

    [CODE=javascript]function test_value(val) {
    return (!isNaN(val) && (val >= -10 && val <= 10));
    }
    [/CODE]
    kind regards

    Comment

    • hello2008
      New Member
      • Dec 2007
      • 14

      #3
      Originally posted by gits
      hi ...

      instead of using a regExp you could use a function like the following:

      [CODE=javascript]function test_value(val) {
      return (!isNaN(val) && (val >= -10 && val <= 10));
      }
      [/CODE]
      kind regards
      Thanks gits,
      It's working fine now...

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        glad to hear that :) ... post back to the forum anytime you have more questions ...

        kind regards

        Comment

        Working...