Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

fix this javascript code

New Here ,
Aug 26, 2009 Aug 26, 2009

var field = form.mpresent; 
    var mpre = parseInt(field.value);  
     if (!mpre) {
    alert("You Must Enter Numeric Value In Members Present Value.");
    field.focus();
    field.select();
    return false;

i had the above code in javascript and it is checking for numeric value to be entered in fiels box in a form but when i entered zero in the field it is poping out the error message but i dont want to give error when i enter zero.

How can i modify this code to check for only not numeric but not for zeroes.

Thanks

TOPICS
Getting started
1.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Aug 26, 2009 Aug 26, 2009

A question more natural to a JavaScript list rathen then a ColdFusion list.

But, What I prefer to do is check the for allowed characters with regex.

Untested code:

var numbers = new RegExp("[^0-9.]"); // Find any character that is not 0-9 or a period.

if (number.test(field.value)){

  alert("Fail, found characters that where something other then 0 through 9 or a decimal point.");

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 26, 2009 Aug 26, 2009

Using the regex and nothing else will pass numbers with more than one decimal place.  Javascript also has parseInt and parseFloat functions that may or may not give you the results you want.

I had to do this once and ended up with about 35 lines of code, including comments and white space that:

Looped through each character of the string and checked to see if it was a number or a decimal point.  If it was anything else, it would immediately return false.

If it was a decimal point, it would increment a counter.  If that counter exceeded 1, the function would immediately return false.

If the string was a legitimate float, the function would return true.

The function got called on onChange.  If it returned false, the value of the form field would be set to 0.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Aug 26, 2009 Aug 26, 2009

Good catch Dan that the regex I provided would allow mutiple decimal points.  It should be possible to construct the regex expression to allow only one decimal, but that is an excersize I leave to others at this moment, I have get started writing that Test Plan I was just assigned.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 03, 2009 Sep 03, 2009

<script type="text/javascript">
function checkVar() {
    var field = document.myForm.mpresent;
    var mpre = parseInt(field.value);
    if (isNaN(mpre)) {
        alert("You Must Enter Numeric Value In Members Present Value.");

        return false;
    }
    return true;
}
</script>
<form name="myForm" >
<input name="mpresent" type="text">
</form>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 03, 2009 Sep 03, 2009

For a good time, test that code with the value "7a".

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 03, 2009 Sep 03, 2009

I wrote it off-the-cuff without testing. Won't be too surprised if there are quirks. This is surely better:

<script type="text/javascript">
function checkVar() {
    var field = document.myForm.mpresent;
    if (isNaN(field.value)) {
        alert("You Must Enter Numeric Value In Members Present Value.");
        field.focus();
        return false;
    }
    else
     var mpre = parseInt(field.value);

    /* etc. etc. */
    return true;
}
</script>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 04, 2009 Sep 04, 2009
LATEST

First things first, Ian is right.  The best place to ask JS question would be on a JS forum, not a CF forum. Still, we can tie CF into this shortly, so that'll get things back on topic.  This isn't to be obstructive, just a tip to think about what one is actually asking about before deciding which forum to ask it on.

Secondly... are you wanting to validate a NUMERIC or an INTEGER?  You're using parseInt(), which would suggest an intenger, but you speak more broadly of numeric values too.

Note to Ian: validating for an int with a regex... you don't want ANY decimal points 😉

Now for the CF tie in.  If you want JS that works with either integer or more general numeric validation, don't try to reinvent the wheel.  CF already provides the JS to do this with <cfinput> and <cfform>.  Even if you don't want to use those constructs, there's nothing to stop you lifting the JS out.

--

Adam

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources