Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13

Thread: Keep input data

  1. #1
    Regular Coder
    Join Date
    Jun 2010
    Location
    Earth
    Posts
    305
    Thanks
    27
    Thanked 2 Times in 2 Posts

    Keep input data

    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

    Code:
    <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>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,155
    Thanks
    203
    Thanked 2,548 Times in 2,526 Posts
    What a ghastly and antiquated email validation.

    Use this regex instead:-

    Code:
    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. Of course a family name may more plausibly include an apostophe - O'Connor etc.
    Last edited by Philip M; 07-20-2012 at 08:24 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Regular Coder
    Join Date
    Jun 2010
    Location
    Earth
    Posts
    305
    Thanks
    27
    Thanked 2 Times in 2 Posts
    LOL

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

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,155
    Thanks
    203
    Thanked 2,548 Times in 2,526 Posts
    Quote Originally Posted by harkly View Post
    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!

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,588
    Thanks
    80
    Thanked 4,497 Times in 4,461 Posts
    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
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,588
    Thanks
    80
    Thanked 4,497 Times in 4,461 Posts
    Nope, I was wrong.

    It is *YOUR CODE* that is doing it!

    Code:
    	if (echeck(emailID.value)==false){
    		emailID.value=""
    		emailID.focus()
    		return false
    	}
    When it doesn't like the email, it clears it out.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,155
    Thanks
    203
    Thanked 2,548 Times in 2,526 Posts
    Quote Originally Posted by Old Pedant View Post
    Nope, I was wrong.

    It is *YOUR CODE* that is doing it!

    Code:
    	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! But in fact that is the sensible thing to do if an invalid email address is entered - require the user to re-enter it.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #8
    Regular Coder
    Join Date
    Jun 2010
    Location
    Earth
    Posts
    305
    Thanks
    27
    Thanked 2 Times in 2 Posts
    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.

    Code:
    <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.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,588
    Thanks
    80
    Thanked 4,497 Times in 4,461 Posts
    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:
    Code:
    	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:
    Code:
    	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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,588
    Thanks
    80
    Thanked 4,497 Times in 4,461 Posts
    Personally, I'd do name validation thus:
    Code:
        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;
        }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #11
    Regular Coder
    Join Date
    Jun 2010
    Location
    Earth
    Posts
    305
    Thanks
    27
    Thanked 2 Times in 2 Posts
    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"

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,588
    Thanks
    80
    Thanked 4,497 Times in 4,461 Posts
    Then why did you have a SPACE in there???

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

    Code:
        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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    harkly (07-21-2012)

  • #13
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,155
    Thanks
    203
    Thanked 2,548 Times in 2,526 Posts
    Quote Originally Posted by Old Pedant View Post
    Personally, I'd do name validation thus:
    Code:
        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.
    Last edited by Philip M; 07-21-2012 at 08:33 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •