Register Blogs FAQ Members List Social Groups Calendar Search Today's Posts Mark Forums Read

Go Back   DeveloperBarn Forums > Programming & Scripting > JavaScript Programming

Sponsored Links

Discuss "textbox validation" in the JavaScript Programming forum.

JavaScript Programming - Javascript is a cross-browser client-side scripting language. Discuss Javascript and AJAX related scripts here.


Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old July 2nd, 2008, 11:02 AM
Barn Frequenter
 
Join Date: Mar 2008
Posts: 197
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 1
todd2006 is an unknown quantity at this point
Default 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;
   }
Reply With Quote
Sponsored Links
  #2  
Old July 2nd, 2008, 11:13 AM
Rebelle's Avatar
Barn Enthusiast
 
Join Date: Mar 2008
Posts: 295
Thanks: 54
Thanked 1 Time in 1 Post
Rep Power: 1
Rebelle is on a distinguished road
Default

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 11:19 AM. Reason: redid
Reply With Quote
  #3  
Old July 2nd, 2008, 11:23 AM
richyrich's Avatar
Super Moderator
 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 456
Thanks: 31
Thanked 42 Times in 42 Posts
Blog Entries: 1
Rep Power: 2
richyrich will become famous soon enoughrichyrich will become famous soon enough

Awards Showcase
JavaScript Classic ASP 
Total Awards: 2

Default

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.
Reply With Quote
  #4  
Old July 2nd, 2008, 12:49 PM
Barn Frequenter
 
Join Date: Mar 2008
Posts: 197
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 1
todd2006 is an unknown quantity at this point
Default

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
Reply With Quote
  #5  
Old July 2nd, 2008, 12:54 PM
richyrich's Avatar
Super Moderator
 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 456
Thanks: 31
Thanked 42 Times in 42 Posts
Blog Entries: 1
Rep Power: 2
richyrich will become famous soon enoughrichyrich will become famous soon enough

Awards Showcase
JavaScript Classic ASP 
Total Awards: 2

Default

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');
        }
Reply With Quote
  #6  
Old July 2nd, 2008, 01:15 PM
Barn Frequenter
 
Join Date: Mar 2008
Posts: 197
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 1
todd2006 is an unknown quantity at this point
Default

i tried it doesnt work
Reply With Quote
  #7  
Old July 2nd, 2008, 01:30 PM
richyrich's Avatar
Super Moderator
 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 456
Thanks: 31
Thanked 42 Times in 42 Posts
Blog Entries: 1
Rep Power: 2
richyrich will become famous soon enoughrichyrich will become famous soon enough

Awards Showcase
JavaScript Classic ASP 
Total Awards: 2

Default

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 With Quote
Reply

  DeveloperBarn Forums > Programming & Scripting > JavaScript Programming

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
checkbox validation todd2006 JavaScript Programming 3 June 18th, 2008 07:22 PM
Textbox or List help Rebelle JavaScript Programming 9 April 21st, 2008 10:00 AM
Programmatically Add Item to Validation Summary jmurrayhead .Net Code Samples 0 March 20th, 2008 09:41 AM


All times are GMT -4. The time now is 04:11 PM.



Content Relevant URLs by vBSEO 3.2.0