#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    1
    Rep Power
    0

    Post How to make allowed values are 1 to 7, n and blank only?


    I am just new with javascript and I only need to allow values when submitted. Allowed values are 1 to 7, n and blank only... How to restrict others? I just started below to allow 1 to 7 but for n and blank, I don't know yet.

    JAVASCRIPT:
    Code:
    function verify(frm) {
    	if (frm.sc_scores.value > 7 || frm.sc_scores.value <= 0) {
    		self.alert(frm.sc_scores.value + " is an invalid value! Allowed values are 1 to 7, n and blank");
    		frm.sc_scores.focus();
    	}
    }

    HTML:
    Code:
    <input type="text" name="sc_scores" id="sc_scores" maxlength="1" size="2" value="<%= number %>" />
    Code:
    <input type="button" onClick="verify(this.form)" value="Edit" />
    Please help... Thanks....
  2. #2
  3. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,173
    Rep Power
    184
    Code:
    <script>
    
    function verify(frm_field)
    {
    
    var allowed = /^[1-7n\s]{1}$/;
    
    var field = document.getElementById(frm_field).value;
    
    if (field.match(allowed)) {
      document.frm.onsubmit = function() {
       return true;
     }
    }
    else if (field.length == 0) {
      document.frm.onsubmit = function() {
       return true;
     }
    }
    else {
     alert(field + " is an invalid value! Allowed values are 1 to 7, n and blank");
    }
    
    }
    
    </script>
    
    
    <form name="frm" onsubmit="return false">
    <input type="text" name="sc_scores" id="sc_scores" maxlength="1" size="2" value="<%= number %>"/>
    <input type="submit" onClick="verify('sc_scores')" value="Edit" />
    </form>
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    @ web_loone08:

    Do you run a "code for free" service now?

    Might be a better idea to explain the approach rather than post solutions. This way the OP might actually learn something apart from copying and pasting stuff from the internet (I'm sure there are better places for that).

    By the way, the "{1}" in the regex is useless. A pattern repeated once is obviously just the pattern.

    Comments on this post

    • web_loone08 agrees : Ikr,
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo