Solved

ASP.net field validation

Posted on 2013-01-24
4
217 Views
Last Modified: 2013-02-15
How do you avoid a user entering more then one word into a text box in asp.net?  Let's say that they can only list 1 word or combination of words but there can be no character spaces between each word?  How would you handle this in ASP.NET?  Thanks!
0
Comment
Question by:VBBRett
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
duttcom earned 500 total points
ID: 38816704
You could add this to your textbox -

onKeyDown="return (event.keyCode!=32);"

As in-

<asp:TextBox ID="CountIDTextBox" runat="server" onKeyDown="return (event.keyCode!=32);"></asp:TextBox>

And then no spaces can be typed into that textbox.
0
 

Author Comment

by:VBBRett
ID: 38816726
Is this C# code or a script?
0
 
LVL 12

Assisted Solution

by:duttcom
duttcom earned 500 total points
ID: 38816742
That's javascript, but it works perfectly in an asp page as per the example.

You can also use onKeyDown="return (event.keyCode!=13);" which will disable the enter key to prevent accidental postback if someone hits the enter key in a textbox.
0
 
LVL 20

Expert Comment

by:informaniac
ID: 38817733
Does this work cross browser?
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question