By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,029 Members | 1,278 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,029 IT Pros & Developers. It's quick & easy.

blank space validation

pradeepjain
100+
P: 563
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

if(document.form1.name.value ==""){
var name=document.form1.name
name.value="";
alert("Please Enter your Name");
name.focus();
return false;
}
Sep 12 '08 #1
Share this Question
Share on Google+
6 Replies


Nepomuk
Expert 2.5K+
P: 3,112
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)
Sep 12 '08 #2

pradeepjain
100+
P: 563
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
Sep 12 '08 #3

Ferris
100+
P: 101
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.
Sep 12 '08 #4

acoder
Expert Mod 15k+
P: 16,027
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.
Sep 12 '08 #5

pradeepjain
100+
P: 563
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
Sep 12 '08 #6

gits
Expert Mod 5K+
P: 5,390
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 ...
Sep 16 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.