1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

JavaScript Numeric Validation

Discussion in 'JavaScript' started by fiz, Nov 9, 2008.

  1. #1
    I'm facing trouble trying to validate a textbox in my code.

    I've found a script that can be used to validate a text field (numerals only), but how can I implement it into my code? I've googled around and see alot of them uses "form", but in my code, there's no form. How am I suppose to integrate it? Help needed. Thanks

    Script I found
    <SCRIPT language="JavaScript">
      <!--
    
    function IsNumeric(strString)
       //  check for valid numeric strings	
       {
       var strValidChars = "0123456789.-";
       var strChar;
       var blnResult = true;
    
       if (strString.length == 0) return false;
    
       //  test strString consists of valid characters listed above
       for (i = 0; i < strString.length && blnResult == true; i++)
          {
          strChar = strString.charAt(i);
          if (strValidChars.indexOf(strChar) == -1)
             {
             blnResult = false;
             }
          }
       return blnResult;
       }
    
      // -->
    </SCRIPT>
    
    Such a function can be used as follows to check a field and return an error message if the contents are not numeric:
    
       if (document.frmUser.afield.value.length == 0) 
          {
          alert("Please enter a value.");
          } 
       else if (chkNumeric(document.frmUser.afield.value) == false) 
          {
          alert("Please check - non numeric value!");
          }
    Code (markup):
    -------------------------------------------

    This is my code. I'd like the text field inputid to be validated, but I tried so many times it didnt work :(
    function ShowInputs()
    {
    	var idx=document.getElementById ( "menu1" ).selectedIndex-1;
    	var valuearray = new Array ();
    	var xaxisarray = new Array ();
    	var inputarray = new Array ();
    	var colorarray = new Array ();
    	var color2array = new Array ();
    	for (i=0;i<=idx;i++) {
    		var xaxisid = "axis"+i;
    		if ( document.getElementById ( xaxisid ) != null ) {
    			if ( document.getElementById ( xaxisid ).value != "" ) {
    				xaxisarray[i] = '<input type="text" id="axis'+i+'" size="3" value="'+document.getElementById ( xaxisid ).value+'" />';
                		}
    		} 
    		else {
    			xaxisarray[i] = '<input type="text" id="axis'+i+'" size="3" value="Q'+i+'" />';
    		}	
    		var inputid = "input"+i;
    		if ( document.getElementById ( inputid ) != null ) {
    			if ( document.getElementById ( inputid ).value != "" ) {
    				inputarray[i] = '<input type="text" onkeyup="valid(this)" onblur="valid(this)" id="input'+i+'" value="'+document.getElementById ( inputid ).value+'"/>';
                		}
    		} 
    		else {
    			if ( i == 0 || i == 1 || i == 2 )
    				inputarray[i] = '<input type="text" onkeyup="valid(this)" onblur="valid(this)" id="input'+i+'" value="50"/>';
    			else
    				inputarray[i] = '<input type="text" onkeyup="valid(this)" onblur="valid(this)" id="input'+i+'" value="Fill in value"/>';
    		}	
    		var colorid = "input_field_"+i;
    		if ( document.getElementById ( colorid ) != null ) {
    			if ( document.getElementById ( colorid ).value != "" ) {
    				colorarray[i] = '<input type="hidden" id="input_field_'+i+'" size="9" value="'+document.getElementById ( colorid ).value+'">';
    				color2array[i] = '<input type="text" disabled="true" id="sample_'+i+'" size="1" value="" style="background-color:'+document.getElementById ( colorid ).value+'" />';
                }
    		} 
    		else {
    			colorarray[i] = '<input type="hidden" id="input_field_'+i+'" size="9" value="#FFFFFF">';
    			color2array[i] = '<input type="text" disabled="true" id="sample_'+i+'" size="1" value="" style="background-color:#FFFFFF" />';
    		}	
    	}
            // reset container content before adding inputs
           	document.getElementById('container').innerHTML = '';
    		
    	for (i=0;i<=idx;i++) {
    		document.getElementById('container').innerHTML += '<b>X-Axis</b>:<br>'+xaxisarray[i]+'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+inputarray[i];
    		document.getElementById('container').innerHTML += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<div id="colorpicker201" class="colorpicker201"></div>';
    		document.getElementById('container').innerHTML += '<input type="button" onclick="showColorGrid2(\'input_field_'+i+'\',\'sample_'+i+'\');" value="Color">';
    		document.getElementById('container').innerHTML += '&nbsp;'+colorarray[i];
    		document.getElementById('container').innerHTML += '&nbsp;'+color2array[i]+'<br>';
    	}
    }
    
    </script>
    Code (markup):
     
    fiz, Nov 9, 2008 IP
  2. balubalu

    balubalu Peon

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If you are interested in client side validation, why don't you just use one of the numerous libraries out there? Live validation comes to mind. Do a google search... once you have it installed, writing the actual validations should take only a few minutes
     
    balubalu, Nov 9, 2008 IP
  3. ferdousx

    ferdousx Peon

    Messages:
    168
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    r u trying 2 findout whether it is a number or not? have u tried isNaN() function? or u r looking for some other type of validation?
     
    ferdousx, Nov 11, 2008 IP
  4. rene7705

    rene7705 Peon

    Messages:
    233
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I'd like to see the HTML that contains id='container'..
     
    rene7705, Nov 11, 2008 IP