+ Reply to Thread
Results 1 to 7 of 7

Thread: textbox validation

  1. #1
    Barn Loyal todd2006 is an unknown quantity at this point todd2006's Avatar
    Join Date
    Mar 2008
    Posts
    889
    Rep Power
    4

    textbox validation

    hi,

    I have a textbox and i want to see the user has a value entered in it

    If he doesnt enter a number value then fire a alert

    so right now I am checking that he doesnt press the space bar and have a empty space

    here is my code.

    But its not working


    Code:
    if(trim(document.frm1.txtnumber.value)
    			{
    				alert("enter a value");	
    			}
    
    
    function Trim(s) 
       {
       	// Remove leading spaces and carriage returns
       	while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
       	 { s = s.substring(1,s.length); }
         
       	// Remove trailing spaces and carriage returns
     while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
       	 { s = s.substring(0,s.length-1); }
         
       	return s;
       }
    

  2. #2
    Barn Legend Rebelle will become famous soon enough Rebelle's Avatar
    Join Date
    Mar 2008
    Posts
    1,522
    Rep Power
    5

    try this:

    Code:
    function isNumeric(elem, helperMsg){
    	var numericExpression = /^[0-9]+$/;
    	if(elem.value.match(numericExpression)){
    		return true;
    	}else{
    		alert(helperMsg);
    		elem.focus();
    		return false;
    	}
    }
    
    Last edited by Rebelle; July 2nd, 2008 at 12:19 PM. Reason: redid

  3. #3
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Try this:-
    Code:
    function check_no(the_id){
    var patt1 = New RegExp("^[0-9]{1,9}$");
     
    var the_value = document.getElementById(the_id).value;
     
    alert(patt1.test(the_value));
    }
    
    This will only allow digits to be in the textbox up to a maximum of 9 digits. Obviously you can change {1,9} to whatever you want the maximum to be.

    To call it have something like:-
    Code:
    <input id="no_only" type="text" size="20" />
    <input id="button1" value="Click" type="button" onclick="check_no('no_only');" />
    
    Hope that helps.

  4. #4
    Barn Loyal todd2006 is an unknown quantity at this point todd2006's Avatar
    Join Date
    Mar 2008
    Posts
    889
    Rep Power
    4

    hi,

    that works out

    Can you guys tell me whats wrong with this code

    I have a textbox if he value is blank or no value or if its $0 then i want to fire the alert

    I have this code

    Code:
    var getfinallingtotal=document.frm1.total.value;
    		
    		If(getfinallingtotal=="" || getfinallingtotal=="$0")
    		{
    					 alert("enter something");
    		}
    
    but it doesnt work any
    idea

  5. #5
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Try using document.getElementById instead, assuming you have given the textbox an id of total. Also try changing the double quotes to single.
    Code:
     
    var getfinallingtotal=document.getElementById('total').value;
     
            If(getfinallingtotal==''||getfinallingtotal=='$0')
            {
                         alert('enter something');
            }
    

  6. #6
    Barn Loyal todd2006 is an unknown quantity at this point todd2006's Avatar
    Join Date
    Mar 2008
    Posts
    889
    Rep Power
    4

    i tried it doesnt work

  7. #7
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Quote Originally Posted by todd2006 View Post
    i tried it doesnt work
    What do you mean "it doesn't work"?

    Do you get any errors? What happens?

    I just tried this:-
    Code:
    function check_box(){
    var getfinallingtotal = document.getElementById('total').value;
     
    if(getfinallingtotal==''||getfinallingtotal=='$0')
    {
    alert('enter something');
    }
    }
     
     
    <input id="total" type="text" size="10" />
    <input id="button1" type="button" value="Check" onclick="check_box();" />
    
    And it worked fine for me.

+ Reply to Thread

Similar Threads

  1. Programmatically Add Item to Validation Summary
    By jmurrayhead in forum .NET Code Samples
    Replies: 3
    Last Post: January 18th, 2011, 01:25 PM
  2. checkbox validation
    By todd2006 in forum JavaScript Programming
    Replies: 3
    Last Post: June 18th, 2008, 08:22 PM
  3. Textbox or List help
    By Rebelle in forum JavaScript Programming
    Replies: 9
    Last Post: April 21st, 2008, 11:00 AM

Bookmarks

Posting Permissions

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

SEO by vBSEO