...

View Full Version : Keep input data



harkly
07-20-2012, 08:03 PM
doing a check email and username script and am wondering how do I get it not to erase the input data when it is wrong?

Give the error message, you click ok and the data stays put until you change it


<script language = "Javascript">

function echeck(str) {

var at="@"
var dot="."
var lat=str.indexOf(at)
var lstr=str.length
var ldot=str.indexOf(dot)
if (str.indexOf(at)==-1){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(at,(lat+1))!=-1){
alert("Invalid E-mail ID")
return false
}

if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(dot,(lat+2))==-1){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(" ")!=-1){
alert("Invalid E-mail ID")
return false
}

return true
}

function checkForm(form) {

var emailID=document.mailcheck.email

if ((emailID.value==null)||(emailID.value=="")){
alert("Please Enter your Email ID")
emailID.focus()
return false
}
if (echeck(emailID.value)==false){
emailID.value=""
emailID.focus()
return false
}

if(form.userID.value == "") {
alert("Error: Please enter your first name");
form.userID.focus();
return false;
}
var alphaExpression = /^[-'a-z ]+$/i;
if(!form.userID.value.match(alphaExpression)) {
alert("Error: First Name - letters, a space and '-' only ");
form.zip.focus();
return false;
}

return true
} // END function checkForm(form)
</script>


<form name="mailcheck" method="post" action="" onSubmit="return checkForm(this)">
Email Address : <input type='text' name='email' class='zip' size='32' ><br>
user: <input type='text' name='userID' class='zip' size='32' ><br>

<input type="submit" name="Submit" value="Submit">
</form>

Philip M
07-20-2012, 08:14 PM
What a ghastly and antiquated email validation. :(

Use this regex instead:-


if (!(/^([a-z0-9])([\w\.\-\+])+([a-z0-9])\@((\w)([\w\-]?)+\.)+([a-z]{2,4})$/i.test(emailaddress.value))) { // returns true if invalid

There is nothing in your code to cause the field to erase if an eror is found.

Form validation of the pattern if (document.formname.formfield.value == "") - that is blank - is barely worthy of the name, and virtually useless, as even a single space, an X or a ? will return false, that is pass the validation. A proper name may only contain letters, hyphen, space and apostrophe.
Numeric values, such as zip codes and phone numbers, should be validated as such. Ditto email addresses. This topic has been covered many times before in this forum.

<script language=javascript> is long deprecated. Use <script type = "text/javascript"> instead (in fact also deprecated but still necessary for IE<9).

Alerts are also obsolete except for test purposes - you should use DOM methods to display a message in a <span>.

These days a first proper name (ethnic??) may include an apostophe. Rah'Nee, A'merika, Shau'Nay. :eek: Of course a family name may more plausibly include an apostophe - O'Connor etc.

harkly
07-20-2012, 08:23 PM
LOL

I just pulled that off of a site, must have been old. Got stuck on the fac that it was refreshing my page.

Philip M
07-20-2012, 08:25 PM
LOL

I just pulled that off of a site, must have been old. Got stuck on the fac that it was refreshing my page.

Yes, very old! Junk it!

Old Pedant
07-20-2012, 08:29 PM
I would *GUESS* that the fields are being "erased" because the <form> is actually being submitted. If you have an error in the JavaScript code, the return checkForm(this) will be ignored and the form will submit anyway.

Since you have action="" for the <form>, that means that it submits back TO THE SAME PAGE that created the <form> and that's probably why you think the form is being erased. Instead, that page just thinks it needs to display the <form> again.

What kind of page is this? HTML? PHP? ASP? If it is an HTML page, then

Old Pedant
07-20-2012, 08:33 PM
Nope, I was wrong.

It is *YOUR CODE* that is doing it!



if (echeck(emailID.value)==false){
emailID.value=""
emailID.focus()
return false
}

When it doesn't like the email, it clears it out.

Philip M
07-20-2012, 08:35 PM
Nope, I was wrong.

It is *YOUR CODE* that is doing it!



if (echeck(emailID.value)==false){
emailID.value=""
emailID.focus()
return false
}

When it doesn't like the email, it clears it out.

Aaarrrgggghh - I did not notice that! :o But in fact that is the sensible thing to do if an invalid email address is entered - require the user to re-enter it.

harkly
07-20-2012, 08:59 PM
Ok so updated the code and I understand wanting the email to erase or the username when it is wrong but this still removing everything when the username is not valid even if the email is valid.


<script type = "text/javascript">

function checkForm(form) {

// checking to see if empty
if(form.email.value == "") {
alert("Error: Please enter your email");
form.email.focus();
return false;
}
// validating that it is in the proper format
var emailValidate = /^([a-z0-9])([\w\.\-\+])+([a-z0-9])\@((\w)([\w\-]?)+\.)+([a-z]{2,4})$/i;
if(!form.email.value.match(emailValidate)) {
alert("Error: Email format");
form.zip.focus();
return false;
}


if(form.userID.value == "") {
alert("Error: Please enter your first name");
form.userID.focus();
return false;
}
var alphaExpression = /^[-'a-z ]+$/i;
if(!form.userID.value.match(alphaExpression)) {
alert("Error: First Name - letters, a space and '-' only ");
form.zip.focus();
return false;
}

return true
} // END function checkForm(form)
</script>


<form name="mailcheck" method="post" action="" onSubmit="return checkForm(this)">
Email Address : <input type='text' name='email' class='zip' size='32' ><br>
user: <input type='text' name='userID' class='zip' size='32' ><br>

<input type="submit" name="Submit" value="Submit">
</form>


Right now I am testing the code in html, it will go into php when i get the html working.

Old Pedant
07-20-2012, 09:13 PM
See my post #5.

You have an error in your JS code, so the form submits. To itself. So the page reloads.

Your error is here:


var alphaExpression = /^[-'a-z ]+$/i;
if(!form.userID.value.match(alphaExpression)) {
alert("Error: First Name - letters, a space and '-' only ");
form.zip.focus();
return false;

There is no such thing as form.zip

By the way, that validation is also worthless.

It allows "-" as a name, for example. It even allows a single space as a name. Surely you want at least a two letter? name?

Try this:


var alphaExpression = /^[a-z]+([-' ][a-z]+)*$/i;

It's still not great, but it's much better. It only allows the space/dash/apostrophe between at least a pair of letters.

Old Pedant
07-20-2012, 09:19 PM
Personally, I'd do name validation thus:


var name = form.userID.value;
var disallow = /[^a-zA-Z\-\s\']/;
if ( disallow.test(name) || name.replace(/[^a-zA-Z]/g,"").length < 5 )
{
alert("Name is invalid");
form.userID.focus();
return false;
}

harkly
07-20-2012, 09:27 PM
Thanks the removal for form.zip worked.

Still learning Javascript, kind-of doing it as I go along, should really devote sometime to just learning that.


And crap! on the regular expression, thought I had that but clearly I do not :(

Trying to get it to be between 8-32 characters with only letters, numbers and . - and _ with no repeating of the last three in a row. So you can have

"User.ID.Whatever"
but not
"User..ID"

Old Pedant
07-20-2012, 10:31 PM
Then why did you have a SPACE in there???

Even your alert mentions that a space is okay. And why the apostrophe?



var name = form.userID.value;
var allowed = /^[a-z0-9]+([\.\-\_][a-z0-9]+)*$/i ;
if ( ! allowed.test(name) || name.length < 8 || name.length > 32 )
{
alert("Name is invalid");
form.userID.focus();
return false;
}

I think that is right.

That says the name must start with letters/digits. Then it allows a REPEAT of zero or more times of ( [ period or dash or underline ] followed by letters/digits ).

Note that only one [ period or dash or underline ] is allowed at at time; at least one letters/digits must follow each one.

Philip M
07-21-2012, 08:28 AM
Personally, I'd do name validation thus:


var name = form.userID.value;
var disallow = /[^a-zA-Z\-\s\']/;
if ( disallow.test(name) || name.replace(/[^a-zA-Z]/g,"").length < 5 )
{
alert("Name is invalid");
form.userID.focus();
return false;
}


A first name might have only two letters - Al, Jo, Ed and so on. And somewhere I read of a man whose first name was X. I believe that ethnic first names may include an apostrophe.

Don't complicate things without a good reason. Keep your userid simple - just letters and numbers. No special characters, periods, apostrophes or underscores. No-one will choose an id of 32 characters. 16 is quite enough.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum