Check if a period '.' is present at input

Discussion in 'JavaScript' started by mogsub, Sep 11, 2009.

  1. #1
    Hi,

    I have a script that checks the input for a mark and does not allow >20 numbers. How can I add to the following code an additional check for the period '.' so that it won't let the user input eg. 12.4?

    function markvalidate(count)
    {
      Form=document.addmarkfrm;
      valid=true;
    
      for(j=1; j<=count; j++)
      {    
        if(document.getElementById('mark-'+j).value=="") { alert("Please Enter Mark"+j); document.getElementById('mark-'+j).focus(); valid=false; return false;}
       else if(document.getElementById('mark-'+j).value!="") { if (!Markcheck(document.getElementById('mark-'+j).value) ) { document.getElementById('mark-'+j).focus(); valid=false; return false;}}
      }
    return valid;
    }
    
    
    function Markcheck(val)
    {
     if(val > 20)
     {
      alert("Please Enter the Mark Below 20");
      return false;
     }else{ return true; }
    }  
    
    
    function checkuploads(mark)
    {
    for (i=0; i<document.addmarkfrm[0].mark.length; i++)
    {
    if (document.addmarkfrm[0].mark[i].value!= '') { user_input = document.addmarkfrm[0].mark[i].value; }
    }
    if (user_input)
    {
    return false;
    }
    }
    
    Code (markup):
    Any help will be appreciated
     
    mogsub, Sep 11, 2009 IP
  2. pneulameiro

    pneulameiro Peon

    Messages:
    440
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    you can use the function indexOf() to see if there is a period in the input.
     
    pneulameiro, Sep 11, 2009 IP
  3. prasanthmj

    prasanthmj Member

    Messages:
    62
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    45
    #3
    What you need is a numeric validation (you don't want to permit input like 2~1,3 too; right?)

    The following check does a numeric validation
    
    if(val.search("[^0-9]")>=0)
    {
    //error
    }
    
    Code (markup):
    This JavaScript form validation script helps in adding validations quickly.
     
    prasanthmj, Sep 11, 2009 IP
  4. mogsub

    mogsub Well-Known Member

    Messages:
    626
    Likes Received:
    42
    Best Answers:
    0
    Trophy Points:
    118
    #4
    I want to insert only numbers from 0.01 to 20.00 and as where I leave the period is used instead of the comma for decimal numbers
     
    mogsub, Sep 11, 2009 IP
  5. Mastermaniac

    Mastermaniac Peon

    Messages:
    128
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Man use the "indexOf()"!

    javascript:var x=prompt("Enter any string");if(x.indexOf(".")==-1){alert("Good there is no '.'")}else{alert("No '.' allowed!")}

    Simple! :D
     
    Mastermaniac, Sep 19, 2009 IP