First Character of form field.

G

Gary

Morning all,

I have a form field called:
Bsk01

How do I onBlur prompt the user to enter a ZERO as character one, if one is
not already entered. At the same time, I would like to ensure at least 5t
characters have been entered. This validation should also only trigger if
anything is entered. Basically, if the user chooses to enter nothing then
the check should not be carried out and the user should be able to continue.

So in pseudo style:

If user data IS ENTERED AND DOES NOT contain a ZERO as first character, or
is LESS THAN 5 characters long, prompt for re-entry.

Thanks guys, been trying various things and got close, but have hit a brick
wall.

This ALMOST works:
function checkField()
{if (document.form1.Bsk01.value.length<9) {alert("Please enter a valid code.
The first character should be a zero.")}else if
(/^0.*$/.test(document.form1.Bsk01.value)){return;}else{alert("Please enter
a valid code. The first character should be a zero.")}}


Gary.
 
G

Gary

This ALMOST works:
function checkField()
{if (document.form1.Bsk01.value.length<9) {alert("Please enter a valid code.
The first character should be a zero.")}else if
(/^0.*$/.test(document.form1.Bsk01.value)){return;}else{alert("Please enter
a valid code. The first character should be a zero.")}}



Sorry should have said. The problem with the above code is that it always
validates, regardless of data being present. If I try and add
if (document.form1.Bsk01.value.length>0 &&
document.form1.Bsk01.value.length<9)
Then the final condition is always returned which always reports to the user
that the data is incorrect. This is because this code says:

If DATA ENTERED > 0 < 5.
Start Check
IF data LESS THAN 5
Report Error
ELSE IF Character one = 0 AND data more than 5.
Return
Else
Report Error due to no 0.

So when a user enters nothing, the first two conditions are not met,
therefore the final; one always applies.

Gary.
 
M

Mick White

Gary said:
Morning all,

I have a form field called:
Bsk01

How do I onBlur prompt the user to enter a ZERO as character one, if one is
not already entered. At the same time, I would like to ensure at least 5t
characters have been entered. This validation should also only trigger if
anything is entered. Basically, if the user chooses to enter nothing then
the check should not be carried out and the user should be able to continue.

So in pseudo style:

If user data IS ENTERED AND DOES NOT contain a ZERO as first character, or
is LESS THAN 5 characters long, prompt for re-entry.

Thanks guys, been trying various things and got close, but have hit a brick
wall.

This ALMOST works:
function checkField()
{if (document.form1.Bsk01.value.length<9) {alert("Please enter a valid code.
The first character should be a zero.")}else if
(/^0.*$/.test(document.form1.Bsk01.value)){return;}else{alert("Please enter
a valid code. The first character should be a zero.")}}


Gary.

First of all, why don't you pass a value to the function?

function checkField(field){
if(!field.value){
return true;
}
msg=""
if (field.value.length<9) {
msg+="Please enter a valid code.\n";
}
if(field.value.charAt(0) != "0"){
msg+="The first character should be a zero";
}
if(msg){
alert(msg);
return false;
}
return true;
}

<input name="Bsk01" type ="text" onblur="checkField(this)">
or
<form onsubmit="return checkField(this.Bsk01)">

Mick
 

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,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top