Prevent elements from clearing after failed validation on nested .NET master page

The validation works perfectly, but if the validation fails, the form clears all data. How can I validate the form without causing the user to have to re-enter all the data again? Due to the nature of the redirect, I need the html form to render exactly as it does now.

HTML:

    <div class="formHalf">
        <label for="first_name">
            First Name*</label>
        <input id="first_name" maxlength="40" name="first_name" size="20" type="text" class="text" />
    </div>
    <div class="formHalf">
        <label for="last_name">
            Last Name*</label>
        <input id="last_name" maxlength="80" name="last_name" size="20" type="text" class="text" />
    </div>
    <div class="formWhole">
        <label for="company">
            Company</label>
        <input id="company" maxlength="40" name="company" size="20" type="text" class="text" />
    </div>
    <div class="formWhole">
        <label for="address">
            Address</label>
        <input id="address" maxlength="80" name="address" size="20" type="text" class="text" />
    </div>
    <div class="formHalf">
        <label for="city">
            City</label>
        <input id="city" maxlength="40" name="city" size="20" type="text" class="text" />
    </div>
    <div class="formHalf">
        <label for="state">
            State/Province</label><input id="state" maxlength="20" name="state" size="20" type="text"
                class="text" />
    </div>
    <div class="formHalf">
        <label for="zip">
            Zip</label><input id="zip" maxlength="20" name="zip" size="20" type="text" class="text" />
    </div>
    <div class="formHalf">
        <label for="country">
            Country</label><input id="country" maxlength="40" name="country" size="20" type="text"
                class="text" />
    </div>
    <div class="formHalf">
        <label for="email">
            Email*</label><input id="email" maxlength="80" name="email" size="20" type="text"
                class="text" />
    </div>
    <div class="formHalf">
        <label for="phone">
            Phone*</label><input id="phone" maxlength="40" name="phone" size="20" type="text"
                class="text" />
    </div>
    <div class="formWhole">
        <label for="description">
            Description</label><textarea name="description"></textarea>
    </div>
    <div class="formWhole">
        <input type="submit"  name="formSubmit" id="formSubmit">
    </div>
</div>`

SCRIPT:

<script type="text/javascript" language="javascript">
   $(document).ready(function () {
        $("#formSubmit").click(function () {
            for (i = 0; i < 1; i++) {
                var isValid = validateForm();
                if (isValid) { }
                  $("form").attr("action", "https://www.other web site that redirects back to my page after logging form");
            }
        });
    });
 function validateForm() {
        var message = "";var message2="";
        var elements = new Array("first_name", "last_name", "phone", "email");
        var userElement = new Array(" First Name", "Last Name", "Phone Number", "E-Mail Address");
        for (i = 0; i < elements.length; i++)
         {
            var x = document.getElementById(elements[i]).value;

            if (x == null || x == "") {
                if (message != "")
                    message += ", "; message += userElement[i];
            }

             if (i == 3 && x != null && x != "") {
                var atpos = x.indexOf("@");
                var dotpos = x.lastIndexOf(".");
                if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= x.length) {
                    message2 += "The email address is not valid.";
                }
            }


            if (i == 2 && x != null && x != "") {
                var checknumber = x.replace(/[^0-9]/g, '');
                if (checknumber.length != 10 && checknumber.length != 11) {
                    message2 += "The phone number is needs to be in format 123-456-7890 or 1-234-567-8901."; 
                }
            }
            // email and phone validation
        }
        if (message != "" || message2 != "") {
            if (message != "") {
                message += " must be filled out.";
            }
            message += message2;
            alert(message);
            return false;
        }
        return true;
    }
</script>`

Answers


The easiest way is just to catch the POST or GET value's on the server's side. When using PHP for example you can refill the First name by adding the following the corresponding input-tag:

value="<?=$_POST['first_name']?>"

You should change the POST tot GET if it's a get-action. Don't forget to additionally validate at the server's side.


if the extension of your page is aspx then add the runat="server" attribute to each data input tag and then you will have the data persisted !


Need Your Help

QRubberBand like feature - Static selection area

c++ qt selection

I'm trying to draw a selection area on top of the desktop/opened windows, which works well by using QRubberBand, but seeing as it does not have a stylesheet command, how would I be able to change the

Quartz scheduler Not Working after exception

java exception weblogic scheduled-tasks quartz-scheduler

I am currently working with Quartz scheduler in my web application to schedule a task after running the server and then repeat it in regular interval.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.