By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,827 Members | 2,140 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,827 IT Pros & Developers. It's quick & easy.

First Character of form field.

P: n/a
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{al ert("Please enter
a valid code. The first character should be a zero.")}}
Gary.
Jul 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
> 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{al ert("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.
Jul 20 '05 #2

P: n/a
Gary wrote:
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{al ert("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

Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.