473,320 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Check textboxes for empty string

Hi
I m using javascript in HTML. I want to know how to validate the text entered in a textbox (for empty strings). My requirement is that my code should not allow empty text (not even spaces -trailing n leading). I have used some code which is checking only wen v do not enter any text. But its not validating when v enter some number of blank spaces.Please help .
Thanks
daitasri
Mar 14 '08 #1
8 27443
vikas1111
122 100+
Hi
I m using javascript in HTML. I want to know how to validate the text entered in a textbox (for empty strings). My requirement is that my code should not allow empty text (not even spaces -trailing n leading). I have used some code which is checking only wen v do not enter any text. But its not validating when v enter some number of blank spaces.Please help .
Thanks
daitasri

Please post your code....
Mar 14 '08 #2
RamananKalirajan
608 512MB
Hi
I m using javascript in HTML. I want to know how to validate the text entered in a textbox (for empty strings). My requirement is that my code should not allow empty text (not even spaces -trailing n leading). I have used some code which is checking only wen v do not enter any text. But its not validating when v enter some number of blank spaces.Please help .
Thanks
daitasri

Hi, for this you can use String function like indexOf('the string you want to check', and the delimiter like space so and so) - if the delimiter is present it will return the index at which it is present otherwise it will return -1.
I give u a code for email validation. This may be helpful for you

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. function validate_email(field,alerttxt)
  5. {
  6. with (field)
  7. {
  8. apos=value.indexOf("@");
  9. dotpos=value.lastIndexOf(".");
  10. if (apos<1||dotpos-apos<2) 
  11.   {alert(alerttxt);return false;}
  12. else {return true;}
  13. }
  14. }function validate_form(thisform)
  15. {
  16. with (thisform)
  17. {
  18. if (validate_email(email,"Not a valid e-mail address!")==false)
  19.   {email.focus();return false;}
  20. }
  21. }
  22. </script>
  23. </head><body>
  24. <form action="submitpage.html"
  25. onsubmit="return validate_form(this);"
  26. method="post">
  27. Email: <input type="text" name="email" size="30">
  28. <input type="submit" value="Submit"> 
  29. </form>
  30. </body></html>
If more doubts means pls post your code
Mar 14 '08 #3
gits
5,390 Expert Mod 4TB
hi ...

have a look at the following example, that uses another simple validation method:

Expand|Select|Wrap|Line Numbers
  1. // our value
  2. var v = ' 1234 ';
  3.  
  4. // the validate function
  5. function validate(val) {
  6.     // first we trim leading and trailing spaces
  7.     val = trim(val);
  8.  
  9.     // now we check for the length of the string
  10.     // and return true when v is not an 'empty string'
  11.     return val.length != 0;
  12. }
  13.  
  14. // trim function that uses regex to replace the spaces
  15. function trim(val) {
  16.     var ret = val.replace(/^\s+/, '');
  17.     ret = ret.replace(/\s+$/, '');
  18.  
  19.     return ret;
  20. }
  21.  
  22. // it will alert true ... so tha validation says ok ...
  23. alert(validate(v));
  24.  
kind regards
Mar 14 '08 #4
Hi

I used the following code ,which trims any number of leading and trailing spaces. I also want to know how to restrict the number of spaces between two words to one.For example :
It should allow strings like "A bc" but not something like "A bc" or "A bc". Please help me on this.It shud not allow two or more spaces between the two words.
Expand|Select|Wrap|Line Numbers
  1.     function trim(str)
  2. {
  3.  
  4.     str=str.replace(/^\s*/,"").replace(/\s*$/,"");
  5.      alert('user id after trim'+str);
  6.   return str;
  7.  
  8. }
Thanks in Advance
daitasri
Mar 17 '08 #5
RamananKalirajan
608 512MB
The following code will remove the white spaces from the begining and end of the String, if it is useful to you just use it

[HTML]<Html>
<head>
<script type="text/javascript">
function Trim(str)
{ while(str.charAt(0) == (" ") )
{ str = str.substring(1);
}
while(str.charAt(str.length-1) == " " )
{ str = str.substring(0,str.length-1);
}
return str;
}
var myString = " This is my string ";
var newString = Trim(myString);
alert("myString length: " + myString.length);
alert(myString);
alert("newString length: " + newString.length);
alert(newString);
</script>
</head>
</html>[/HTML]
Mar 17 '08 #6
gits
5,390 Expert Mod 4TB
... I also want to know how to restrict the number of spaces between two words to one.For example : It should allow strings like "A bc" but not something like "A bc" or "A bc". Please help me on this.It shud not allow two or more spaces between the two words.
...
here is an example:

Expand|Select|Wrap|Line Numbers
  1. var s = 'A    bc';
  2.  
  3. function trim_inner_spc(s) {
  4.     s = s.replace(/\s+/g, ' ');
  5.     return s;
  6. }
  7.  
  8. alert(trim_inner_spc(s));
  9.  
use it after trimming the trailing and leading spaces ...

kind regards
Mar 17 '08 #7
here is an example:

Expand|Select|Wrap|Line Numbers
  1. var s = 'A    bc';
  2.  
  3. function trim_inner_spc(s) {
  4.     s = s.replace(/\s+/g, ' ');
  5.     return s;
  6. }
  7.  
  8. alert(trim_inner_spc(s));
  9.  
use it after trimming the trailing and leading spaces ...

kind regards

Hi
Thanks a lot .. Its working perfectly.
Regards
daitasri
Mar 17 '08 #8
gits
5,390 Expert Mod 4TB
no problem ... post back to the forum anytime you have more questions ...

kind regards
Mar 17 '08 #9

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

Similar topics

1
by: Newbie | last post by:
hello, i have a 14 textboxes and 14 requiredfieldvalidator controls to validate the textboxes. if one or more of the textboxes is empty, the corresponding requiredfieldvalidator controls show...
4
by: jw56578 | last post by:
I want to iterate through the textboxes on a page and set their text to an empty string. I do this on pre render. but if i use the following code i cannot clear the text because a Control object...
5
by: Shapper | last post by:
Hello, I am using this code line to check i a variable exists: If cookie Is Nothing Then .... How can I check if cookie is empty when cookie exists? Thanks, Miguel
0
by: Coco | last post by:
Hi, who knows how to update the 'child' textboxes in a datarelated situation if the child records "doesn't yet exitsts/are still empty"? The problem can be explained with the Northwind database....
1
by: Roy | last post by:
Hey All, Kind of a series of anomalies here with one root source, I believe. I have an editable, paging datagrid. 1.When one clicks "edit" textboxes open up on row x, HOWEVER, if one clicks...
4
by: Brett Romero | last post by:
Besides using a very long if statement and storing old text box values, is there a way to see if text box values have changed since an event? For example, say I have 8 textboxes when the form...
4
by: Drum2001 | last post by:
SELECT ra_report.CCENTER, FROM ra_report WHERE (((ra_report.CCENTER) Like "*" & !! & "*" Or (ra_report.CCENTER) Like "*" & !! & "*" Or (ra_report.CCENTER) Like "*" & !! & "*" Or...
11
by: Kevin O'Brien | last post by:
Hello, I am creating a sign on screen for my application in which I want to store the username and password in a database table. I was thinking of putting a combo box connected to the database...
0
by: Matthew87 | last post by:
Hi all I'm having a bit of trouble completeing my coursework basically so far i have created Citenary class with support methods for a private array. The problem is i can add objects to the array...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.