Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Dec 2009
    Posts
    42
    Thanks
    13
    Thanked 0 Times in 0 Posts

    JS restrictive validation - decimal

    Hi,
    Dreamweaver generated the javascript for an onBlur validation for an input textbox. Is it possible to change this code so that I do not have a range but, instead, specific numbers that must be input by the user? I want to ensure that the users input integers and only the following decimals: .25; .5, and
    .75.

    [function MM_validateForm() { //v4.0
    if (document.getElementById){
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
    if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
    if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
    } else if (test!='R') { num = parseFloat(val);
    if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
    if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
    min=test.substring(8,p); max=test.substring(p+1);
    if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
    } if (errors) alert('The following error occurred:\n'+errors);
    document.MM_returnValue = (errors == '');
    } }
    </script>]

    John

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,554
    Thanks
    80
    Thanked 4,620 Times in 4,583 Posts
    DrunkWalker creates some of the most gawdawful JS code you'll ever see.

    That email validation that is part of the code you show is, for example, so inadequate it is worthless. Ditto its validation of text fields. And, as you noted, its validation of numbers is nearly pointless. Doesn't even allow you to insist on integers, for example.

    It's way way past time to get rid of DreadedWeasel code whenever possible.

    Yes, you can easily build JavaScript to validate most anything you can think of.

    Example to meet your specs:
    Code:
    <html>
    <head>
    <script type="text/javascript">
    function checkQuarters( fld )
    {
        var val = parseFloat(fld.value);
        if ( isNaN(val) || ( val*4 != Math.floor(val*4) ) )
        {
            alert("Value must be a multiple of 0.25");
            return false;
        }
        return true;
    }
    </script>
    </head>
    <body>
    <form>
    Amount, to nearest 25 cents: 
       <input name="whatever" onchange="checkQuarters(this);" />
    </form>
    </body>
    </html>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    JohnShell (08-27-2010)

  • #3
    Regular Coder
    Join Date
    Jul 2003
    Posts
    117
    Thanks
    0
    Thanked 17 Times in 17 Posts
    I want to ensure that the users input integers and only the following decimals: .25; .5, and .75.
    Why not make it a dropdown?

    Typing in some value and then the validation saying that the 'value is not good ' makes it frustrating.

  • Users who have thanked prasanthmj for this post:

    JohnShell (08-27-2010)

  • #4
    New Coder
    Join Date
    Dec 2009
    Posts
    42
    Thanks
    13
    Thanked 0 Times in 0 Posts
    Perfect! Thank you very much. BTW - you had me with DrunkWalker until I saw DreadedWeasel and realized you meant DreamWeaver - LOL on that one.
    Again, thanks - works great.

  • #5
    New Coder
    Join Date
    Dec 2009
    Posts
    42
    Thanks
    13
    Thanked 0 Times in 0 Posts
    I appreciate your suggestion but, it would encompass too many possible selections in this instance. Old Pendant's code works perfectly for my needs. Again, thank you.


  •  

    Posting Permissions

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