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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

input=text -> validate on xx,x 1

Status
Not open for further replies.

Bramvg

IS-IT--Management
Jan 16, 2001
135
0
0
BE
Hi,

does anyone of you know how to validate a text field that must be formatted like xx,x.

With many thanks
bram
 
Just a quick one from my head, haven't tried it...

First of all, set the maxlength=4, so they don't write anything else.
Second of all,
if(
isNaN(txtfield.charAt(0))||
isNaN(txtfield.charAt(1))||
txtfield.charAt(2) !=","||
isNaN(txtfield.charAt(3))
)
{
//error handling, format is not matching
}
else
{
//do what ever you want to do, format is matching
}

or something this way...??
My codes look like something a kid wrote
I have absolutely no idea what I am talking about
Somehow I still manage to make it work
 
hie

xx,x. ... is that last dot neccessary?

what are those x-s ?

if they are digits, here you are:

<script>
var str=&quot;12,7.&quot;
var re=/^\d{2}\,\d\.$/
if (str.match(re)) alert(&quot;yup&quot;)
else alert(&quot;nope&quot;)
</script>

for any word character there would be \w instead of \d
& so on Victor
 
hi Vituz

I'm sorry, but I don't understand exactly what you've sent.
Most likely because I'm not familiar with jscript.

anyhow, I explain.
I have a text field, called 'taxes'.
When a user enters e.g.

147 it should be changed into 14,7 OR
1,47 it should be changed automatically into 14,7 OR
14 it should be changed into 14,0

Meaning:

Min (and max) 3 numbers (or 4 chars)
and the format is xx,x

= [number][number],[number]

Any help more than welcome
bram


 
ok, got it :)

here is an example:
~~~~~~~~
<html><head><title>validate to dd,d</title>
<script>
<!--
function validate(obj){
/*regular expression:
^ - begining of the string
\d - any digit (0..9)
{x} - repeat x times
\, - just a ,
*/

var re=/^\d{2}\,\d$/
var str=obj.value
if (!str) {alert(&quot;enter something&quot;);obj.focus(); return false}
/*
if that expression would match the string,
then everything's good, we don't have to change anything
*/

if (!str.match(re)){
/* everythings baaaad, we have to get up, put our fingers
out of our nose & do something (should we?) */
//kicking out everything but digits

var tempstr=str.replace(/\D/g,'')

var len=tempstr.length
if (len<3){
//too short - lets add zeros
for (var ii=len; ii<=3; ii++) tempstr+=&quot;0&quot;
}else{
//too long - would be truncated
tempstr=tempstr.substr(0,3)
}
//making it look like you want:
tempstr=tempstr.replace(/(\d{2})(\d)/,'$1,$2')
obj.value=tempstr
}
}
//-->
</script>
</head>
<body>
<form name=&quot;form1&quot;>
<input type=text name=txt onchange=&quot;validate(this)&quot;><br>
<input type=button onclick=&quot;validate(this.form.txt)&quot; value=&quot;validate&quot;>
</form>
</body>
</html>
~~~~~~~~ Victor
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top