form validation

M

Martin Nadoll

Hello,

i need to change and than validate a form value onSubmit so that one field
called blz accepts whitespace, but submits the value without whitespace and
checks it for only numbers and length=8, so that:
"500 100 60" is valid, and becomes "50010060" because after filtering
whitespace, the entry is 8 digits long.
"500-100-60" is not valid and produces a alert.
a second formfield accepts Whitespace and "-" but also only submits numbers
(there must be exact 10 digits) and checks for them after filtering " " and
"-".
So it should accept "1234567-890", "12 34 56 78-90" and submits "1234567890"
but dont accept letters an other characters.

How do i do that?

Thanks for any help,
Martin Nadoll
 
E

Evertjan.

Martin Nadoll wrote on 30 okt 2003 in comp.lang.javascript:
Hello,

i need to change and than validate a form value onSubmit so that one
field called blz accepts whitespace, but submits the value without
whitespace and checks it for only numbers and length=8, so that:
"500 100 60" is valid, and becomes "50010060" because after filtering
whitespace, the entry is 8 digits long.
"500-100-60" is not valid and produces a alert.
a second formfield accepts Whitespace and "-" but also only submits
numbers (there must be exact 10 digits) and checks for them after
filtering " " and "-".
So it should accept "1234567-890", "12 34 56 78-90" and submits
"1234567890" but dont accept letters an other characters.

<script>

function validate(t) {
t = t.replace(/[ -]+/g,"")
if (/^\d{10}$/.test(t))
return t
else
return false
}

// function returns the 10 digit string or false

// tests:
alert(validate("0123---4 - 456789")+"\n0123---4 - 456789")
alert(validate("0123---4 - 56789")+"\n0123---4 - 56789")
alert(validate("123")+"\n123")


</script>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top