Connecting Tech Pros Worldwide Help | Site Map

blank space validation

pradeepjain's Avatar
Needs Regular Fix
 
Join Date: Jul 2007
Location: India
Posts: 407
#1: Sep 12 '08
Hii guys,
I am using javascript validation..i am able to validate blank fields but if he enters a space in that field its not getting validated ..how to do it

Quote:
if(document.form1.name.value ==""){
var name=document.form1.name
name.value="";
alert("Please Enter your Name");
name.focus();
return false;
}
Nepomuk's Avatar
Moderator
 
Join Date: Aug 2007
Location: Germany
Posts: 2,466
#2: Sep 12 '08

re: blank space validation


As this is a JavaScript question and not a Java issue, I'm moving it to the JavaScript Forum. Please have a look at those two links to wikipedia, if you're unsure about the difference. They are two completely different languages.

Greetings,
Nepomuk (Moderator)
pradeepjain's Avatar
Needs Regular Fix
 
Join Date: Jul 2007
Location: India
Posts: 407
#3: Sep 12 '08

re: blank space validation


ok sorry for that.


i found this solution..it works fine
Expand|Select|Wrap|Line Numbers
  1. function isEmpty( str){
  2.     strRE = new RegExp( );
  3.     strRE.compile( '^[\s ]*$', 'gi' );
  4.     return strRE.test( str.value );
  5. }
  6.  
but i wanted to know what exactly he is doing ..can any one explain me....


Thanks,
Pradeep
Ferris's Avatar
Member
 
Join Date: Oct 2007
Location: Shanghai
Posts: 102
#4: Sep 12 '08

re: blank space validation


Quote:

Originally Posted by pradeepjain

ok sorry for that.


i found this solution..it works fine
function isEmpty( str){
strRE = new RegExp( );
strRE.compile( '^[\s ]*$', 'gi' );
return strRE.test( str.value );
}

but i wanted to know what exactly he is doing ..can any one explain me....


Thanks,
Pradeep


Hi
the string '^[\s ]*$' is a regular expression.
[\s] matches the blank character.
[\s]* matches many blank characters.
^[\s]* matches at the start of the blank string,
[\s]*$ matches at the end of the blank string.
So , ^[\s ]*$' matches at the start of the blank string till the end of the blank string. It matches the blank string.

if "str.value" is a blank string , it will match the regular pattern '^[\s ]*$' . Then "strRE.test( str.value )" will be true.

You can have a look at Regular Expression syntax reference.
http://www.regular-expressions.info/reference.html

hope it helps.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#5: Sep 12 '08

re: blank space validation


pradeepjain, as a full member for a long time now, you should know that we expect your code to be posted in [code] tags (See How to Ask a Question).

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

Please use the tags in future.

Moderator.
pradeepjain's Avatar
Needs Regular Fix
 
Join Date: Jul 2007
Location: India
Posts: 407
#6: Sep 12 '08

re: blank space validation


Quote:

Originally Posted by acoder

pradeepjain, as a full member for a long time now, you should know that we expect your code to be posted in [code] tags (See How to Ask a Question).

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

Please use the tags in future.

Moderator.


hey really sorry for that .I think by mistake i clicked on QUOTE tag instead of CODE tag..i will see to that it doesn't happen again


thanks,
Pradeep
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,128
#7: Sep 16 '08

re: blank space validation


hi ...

the shown code could (and probably should) be improved in the following manner:

Expand|Select|Wrap|Line Numbers
  1. function isEmpty( str ){
  2.     var strRE = /^[\s ]*$/gi;
  3.     return strRE.test( str );
  4. }
first we should always declare the variables in functions by using the var keyword ... otherwise we would create a global variable which could cause problems later on and we should just avoid this. next we don't need the constructor and a function call to create a regExp instance ... we just use the literals for better performance ...

kind regards

PS: adapt this dependent to what input will be passed to the function but it would be better to pass the value itself instead of a dom-node so that the function doesn't need to know what it should evaluate ... just for the case the function should be reused for other cases later on ...
Reply


Similar JavaScript / Ajax / DHTML bytes