HTML Tutorial


 Forum HomeForum Home   FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
RegisterRegister - Not registered yet? Got something to say? Join HTML Code Tutorial!
Trouble in Validation
Post new topic   Reply to topic    HTML Help Forum Index -> HTML Form
View previous topic :: View next topic  
Author Message
rajatgupta_iitr



Joined: 13 Sep 2006
Posts: 2

PostPosted: Mon Sep 18, 2006 5:00 am     Trouble in Validation Reply with quote

i need to ensure an entry in a text box.at the same I need to ensure that the entry is not in the form of spaces i.e. it needs to be some alphabets or numbers or special characters only.
I am giving the function that I invoked by onSubmit

function ensure_item()
{
if(formname.textitemname.value=="")
{alert("You must enter a value for Item");
formname.textitemname.focus();
return false;}
else return true;
}

and I simply used onSubmit="ensure_item()" in the form tag.

Now the code works as long as user types'nothing' in the test box but if he types a series of spaces the code no longer works and validation fails.

Please help...i wd really appreciate if some modifications in the code i gave are suggested.

thanks in advance
Rajat
degsy



Joined: 23 Feb 2005
Posts: 2440
Location: North East, UK

PostPosted: Mon Sep 18, 2006 5:25 am     Reply with quote

For a basic hardcoded script you can simply check for a space.

Code:

<script type="text/javascript">
function ensure_item(){
   if(!document.formname.textitemname.value){
      alert('textitemname is a required field');
      return false;
   }
   else if(document.formname.textitemname.value.indexOf(' ') != -1){
      alert('textitemname cannot contain spaces');
      return false;
   }
}
</script>
<form name="formname" method="post" action="" onsubmit="return ensure_item()">
  <input name="textitemname" type="text" id="textitemname">
  <input type="submit" name="Submit" value="Submit">
</form>



If you have further requirements then it may be best looking into Regular Expression validation.
rajatgupta_iitr



Joined: 13 Sep 2006
Posts: 2

PostPosted: Tue Sep 19, 2006 2:34 am     @degsy Reply with quote

hi,
Thaks for ur help in validation problem.Can u tell ime if I can also use 'trim' function in the same context i.e. something like
if(!Trim(formname.field.value)=="')
{
alert("alert text");
return false;
}

I tried using this but it doesn't seem to work...maybe some syntactic error because I m very new to javascript and programming in general.
Actually the reason for wanting to know this is that I think if I use Trim then even if the user enters some spaces followed by commas then the function wd work .
the solution u provided works if there are all spaces but if there is some text along with commas it still alerts the user and forces him to remove spaces which I do not want.
My intention is only to ensure that some text be ther...with or without spaces but not just spaces.

it wd be great if u cd help
degsy



Joined: 23 Feb 2005
Posts: 2440
Location: North East, UK

PostPosted: Thu Sep 21, 2006 4:58 am     Reply with quote

Javscript doesn't have a built in trim function.

You can create one though
http://www.faqts.com/knowledge_base/view.phtml/aid/1678/fid/1
Display posts from previous:   
Post new topic   Reply to topic    HTML Help Forum Index -> HTML Form All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
HTML Help Archive
Powered by phpBB © 2001, 2005 phpBB Group
HTML Help topic RSS feed 

 
DARFUR
HOSTING / DESIGN
MAKE MONEY

Home
  |   Tutorials   |   Forum   |   Quick List   |   Link Directory   |   About
Copyright ©1997-2002 Idocs and ©2002-2007 HTML Code Tutorial