Web Analytics Made Easy -
StatCounter newbie: failure to understand forms and validation with regular expressions - CodingForum

Announcement

Collapse
No announcement yet.

newbie: failure to understand forms and validation with regular expressions

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • newbie: failure to understand forms and validation with regular expressions

    Code:
    <HTML>
    <BODY>
    <SCRIPT LANGUAGE="JavaScript">
    <!-- Hide code from older browsers
    
    var num1 = new RegExp("[0-9]", "g")
    function gears() {
       if (!num1.test(document.gears1.ring1.value)) {
          alert("bad" + document.gears1.ring1.value + "<<")
          return false
       }
    alert("good" + document.gears1.ring1.value + "<<")
    return false
    }
    // End hiding-->
    </SCRIPT>
    <FORM onsubmit="return gears()" NAME="gears1">
    <P>
    <INPUT TYPE="TEXT" SIZE="10" NAME="ring1">
    <INPUT TYPE="BUTTON" VALUE="Calculate" ONCLICK="gears()">
    </FORM>
    </BODY>
    </HTML>
    Gives two boxes; entering two digits into the left one and repeatedly pressing 'calculate' gives me two alerts 'good' followed by one 'bad', then repeats. Three digits, three 'good', one 'bad' Why?

  • #2
    I don't know what code you're looking at, because I ran your code and I have ONE text box, not two.

    Also, if I enter a numeric value (ie. 222) I get a good#<< (ie. good222<<). If I don't enter a numeric value, I get a bad#<< (ie. entered bb2, got badbb2<<).

    I'm not sure where your problem is, or what you're looking for exactly.

    Sadiq.

    Comment


    • #3
      Code:
      <HTML>
      <BODY>
      <SCRIPT LANGUAGE="JavaScript">
      <!-- Hide code from older browsers
      
      
      alert( num1 );
      function gears( f )
      {
      	var pattern = /^[0-9]+$/;
      	var field = f.ring1;
      
      	if ( !pattern.test( field.value ) )
      	{
      		alert("bad >>" + field.value + "<<")
      		return false;
      	}
      	alert( "good >>" + field.value + "<<")
      	return false;
      }
      // End hiding-->
      </SCRIPT>
      <FORM onsubmit="return gears( this )" NAME="gears1">
      <P>
      <INPUT TYPE="TEXT" SIZE="10" NAME="ring1">
      <INPUT TYPE="BUTTON" VALUE="Calculate" ONCLICK="gears( this.form )">
      </FORM>
      </BODY>
      </HTML>
      My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
      “Minds are like parachutes. They don't work unless they are open”
      “Maturity is simply knowing when to not be immature”

      Comment


      • #4
        What about?:

        <HTML>
        <BODY>
        <SCRIPT LANGUAGE="JavaScript">
        <!-- Hide code from older browsers


        alert( num1 );
        function gears( f )
        {
        var pattern = /^[0-9]+$/;
        var field = f.ring1;

        if ( !pattern.test( field.value ) )
        {
        alert("bad >>" + field.value + "<<");
        field.value = field.value.replace(/\D/g,"");
        return false;
        }
        alert( "good >>" + field.value + "<<");
        return true;
        }
        // End hiding-->
        </SCRIPT>
        <FORM onsubmit="return gears(this)" NAME="gears1">
        <P>
        <INPUT TYPE="TEXT" SIZE="10" NAME="ring1" onkeyup="gears(this.form)">
        </FORM>
        </BODY>
        </HTML>
        KOR
        Offshore programming
        -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

        Comment


        • #5
          Thank you, the two solutions will be most helpful.

          But I'm afraid it's only half the problem; your code is different, but what I don't understand is why my original code works the way it does, or why it has to be coded the way you've done it. Which means I'll do it again Would you be kind enough to explain what's wrong with the original?

          Comment

          Working...
          X