Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Textbox validation asp.net

Status
Not open for further replies.

LauraCairns

Programmer
Jul 26, 2004
173
0
0
GB
I have the following textbox in the footer of a Datagrid and I'd like to try and add some client side code too take out all the spaces which the user might enter by accident. The textbox is used to enter forename text so basically I need the code to strip out any spaces a user may enter. At the moment I have two validators and my html code looks as follows: -
Can someone help me enter the code I need to achieve this.
Thanks very much for your help

<FooterTemplate>
<asp:TextBox ID="add_Forename" Columns="5" Runat="Server" />

<asp:RequiredFieldValidator id="rvForename" runat="server" ErrorMessage="A Forename is required" ControlToValidate="add_Forename"
Font-Size="Small">*
</asp:RequiredFieldValidator>

<asp:regularexpressionvalidator id="revlLengthLetter" ForeColor="Red" Font-Size="Small" runat="server" ErrorMessage="Forename must be more than 2 and less than 25 characters and has to be a letter" ControlToValidate="add_Forename" ValidationExpression="^[A-Za-z]{2,25}$">*
</asp:regularexpressionvalidator>
</FooterTemplate>
 
You can do things like that with JavaScript


<SCRIPT LANGUAGE="JavaScript">

function CheckValidName(textbox, val){
val = val.replace(/[^a-zA-Z]/g, ''); // strip non-alpha chars
textbox.value = val; // replace textbox value

}

</SCRIPT>

Then on the form for the required field use the onkeyup attribute

not sure if it works for asp.net so you'll have to try it..

<asp:TextBox ID="add_Forename" Columns="5" Runat="Server" onKeyUp="CheckValidAdd(add_Forename, add_Forename.value);">/>

this only allows alpha numeric characters.

 
<asp:TextBox ID="add_Forename" Columns="5" Runat="Server" onKeyUp="CheckValidAdd(add_Forename, add_Forename.value);">

sorry just noticed my typo
 
I tried the following below and got a Compilation Error
on the textbox line. Any ideas. There is no onKeyup event.
Cheers for your help so far. I'm not very good at all with Javascript

<script language="javascript" type="text/javascript">
function CheckValidAdd(textbox, val)
{
val = val.replace(/[^a-zA-Z]/g, '');
textbox.value = val; // replace textbox value
}
</script>

<asp:TextBox id="TextBox1" runat="server" AutoPostBack="True" OnTextChanged="CheckValidAdd(TextBox1,
TextBox1.value);"></asp:TextBox>

 
<asp:TextBox ID="LocalEven" Runat="server" onkeyup="calcTotal(this.form)"></asp:TextBox>

I found this on another site showing you can use onkeyup event attribute ?
 
Loving your work!! Cheers its not an option but i used it anyone and it worked grand. Thanks
 
I noticed you have changed the function to CheckValidAdd

If you are using it for addresses remember they need to allow number and surnames can be hyphenated so i use a varied set of functions here they are and what they do ...

----- this allows number and letters

function CheckValidCharNum(textbox, val){
val = val.replace(/[^a-zA-Z\s0-9]/g, ''); // strip non-alphanumeric chars
textbox.value = val; // replace textbox value

}

----- this allows ampersands and dots as sell as alpha numeric

function CheckValidCompany(textbox, val){
val = val.replace(/[^a-zA-Z\s0-9&.]/g, ''); // strip non-alphanumeric chars allowing ampersand + dot
textbox.value = val; // replace textbox value

}

----- this allows alphanumeric + spaces


function CheckValidPost(textbox, val){

val = val.replace(/[^0-9a-zA-Z\s ]/g, ''); // strip non-alphanumeric chars but allow space


textbox.value = val; // replace textbox value

}

----- this allows alphanumeric and the hyphen (-)

function CheckValidName(textbox, val){
val = val.replace(/[^a-zA-Z\s-]/g, ''); // strip non-alpha chars allowing minus for double barrel
textbox.value = val; // replace textbox value

}

-------------------------------------------
hopefully you will see how the REG-EX is being used on the replace command in the javascript and you can change to suit your needs.

enjoy :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top