473,372 Members | 1,049 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,372 software developers and data experts.

Format textbox value using regexpression

I need to format a textbox value a user enters. The user will enter 13
characters, and then I have to format them, preferably in the onBlur event,
to look like this:

XXXX-XX-XXX-XXXX

I found a script that does commas using Regular expresssions:
Jul 20 '05 #1
3 9438
Unfortunately, I hit send before I was ready. Here is the rest of my post:

I found a script that does commas using Regular expresssions:
http://javascript.internet.com/forms/commas.html

If I knew the right regular expression for my situation, I could modify this
code to work for me. Can anyone help?

thank you

"H Branyan" <hb****************@merc.mercer.edu> wrote in message
news:bn************@ID-92475.news.uni-berlin.de...
I need to format a textbox value a user enters. The user will enter 13
characters, and then I have to format them, preferably in the onBlur event, to look like this:

XXXX-XX-XXX-XXXX

I found a script that does commas using Regular expresssions:

Jul 20 '05 #2
"H Branyan" <hb****************@merc.mercer.edu> writes:
I need to format a textbox value a user enters. The user will enter 13
characters, and then I have to format them, preferably in the onBlur event,
Why not the "onchange" event?
to look like this:

XXXX-XX-XXX-XXXX
How is the text input? If it is just 13 characters, then it is fairly
easy. If the user inputs the charaters with "-"'s in between, you
might need to remove them first. What characters are legal? If only
letters and digits, you can remove all other punctuation first. You
should expect the string to contain hyphens, if the user changes it
after you have formatted it once.
I found a script that does commas using Regular expresssions:


I wouldn't bother with regular expressions when you know where to
split the string.

If there are just 13 characters in the input element, you can do like
this:
---
var str = element.value;
str = str.substr(0,4) + "-" + str.substr(4,2) + "-"
str.substr(6,3) + "-" + str.substr(9,4);
element.value = str;
---
If you need to remove non-alphanumeric characters first, then you
can use a regular expression. Change the first line to

var str = element.value.replace(/[^\w]+/g,"");

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3
This info should be enough for me to get what I want done. I appreciate
your quick and thorough response.
"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:k7**********@hotpop.com...
"H Branyan" <hb****************@merc.mercer.edu> writes:
I need to format a textbox value a user enters. The user will enter 13
characters, and then I have to format them, preferably in the onBlur
event,
Why not the "onchange" event?
to look like this:

XXXX-XX-XXX-XXXX
How is the text input? If it is just 13 characters, then it is fairly
easy. If the user inputs the charaters with "-"'s in between, you
might need to remove them first. What characters are legal? If only
letters and digits, you can remove all other punctuation first. You
should expect the string to contain hyphens, if the user changes it
after you have formatted it once.
I found a script that does commas using Regular expresssions:


I wouldn't bother with regular expressions when you know where to
split the string.

If there are just 13 characters in the input element, you can do like
this:
---
var str = element.value;
str = str.substr(0,4) + "-" + str.substr(4,2) + "-"
str.substr(6,3) + "-" + str.substr(9,4);
element.value = str;
---
If you need to remove non-alphanumeric characters first, then you
can use a regular expression. Change the first line to

var str = element.value.replace(/[^\w]+/g,"");

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors:

<URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html> 'Faith without judgement merely degrades the spirit divine.'

Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: ST | last post by:
Hello, I will be posting 2 errors that I just can't seem to figure out (I'll put them in different posts). I have looked all over the internet, and I still can't figure these out! This webapp was...
8
by: Assimalyst | last post by:
Hi i have a value entered into an asp text box, procedureDateTxtBx.Text, thet has the format dd/MM/yyyy. I need to convert this into a format recognisable by SQL server in order to properly query...
4
by: Jan Nielsen | last post by:
Hi I have a dataform that shows a date from a MSDE 2000 in a textbox. It uses the format: dd-mm-yyyy hh:mm:ss It would it to use the format dd-mm-yyyy But I can't find a Format property for the...
1
by: Rich | last post by:
Hello, I have some datefields in a dataset (ds1). I bind some textbox controls on a windows form to these date fields in ds1, but I only want to see 01/01/2004 instead of 1/1/2004 8:00:00 AM. ...
7
by: Richiep | last post by:
I am trying to get a UK format date of dd/mm/yyyy. Why does the following subroutine not return a valid date in a web form? The date returned is #12:00:00 AM# but the date I entered into the...
11
by: RipperT | last post by:
Don't know if this group covers web apps, but here goes. In VS 2005, I am trying to get variables to hold thier values during postback from the server. I convert a text box's user-keyed value to an...
2
by: Lyn | last post by:
Hi, Having fun trying to get Conditional Formatting working on a textbox control in a continuous form subform (Access 2003). The condition I want is when the value of the textbox is Null and/or...
9
by: Mark G. | last post by:
Good afternoon. I have a "Fixed/2" format applied to a textbox on its property sheet. When I type data in the textbox, it is formatted correctly. But when I update the value programatically by...
7
by: pamela fluente | last post by:
My numericUpDowns show numbers in the format 1.500,56 (italy). Instead, I need *invariantly* that they show and accept the format 1,500.56, as in the USA. What's the right way to do that? I...
1
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
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.