April 16, 201412 yr This is my jquery: $(document).ready(function () { $('#fname').change(function () { x = $(this).val(); if (x == null || x == "") { y = document.write("eroor") } }); }) the above is in my head section of the html and I want to display y in an element in my html body. tnx
April 16, 201412 yr Something like this? http://jsfiddle.net/LyndseyB/8fe7F/ I've added a container div and some classes so that you can basically duplicate your HTML to another select element and also validate that with the same code. Hope it helps.
April 17, 201412 yr Author Something like this? http://jsfiddle.net/LyndseyB/8fe7F/ I've added a container div and some classes so that you can basically duplicate your HTML to another select element and also validate that with the same code. Hope it helps. Ok would work but look at what I ve tried doing: Jquery $(document).ready(function(){ $("#email").change(function(){ var em = $(this).val(); var atpos=em.indexOf("@@"); var dotpos=em.lastIndexOf("."); if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= x.length) { $("#m").text("Please enter a valid email") } else { $("#email").change(function () { $("#m").fadeOut(); }) } }) }) HTML <tr><td><label for="email">Email</label></td><td> <input value="@Request.Form["email"]" type="text" name="email" style="text-transform:lowercase" placeholder="Email" id="email" /></td><td id="m">@Html.ValidationMessage("email")</td><td id=""></td></tr> It worked for validating email but if I correct the email to valid format, the "else" part of the if statement in the jquery doesnt reflect. Thanks a lot.
April 17, 201412 yr To be honest, I don't think it's worth the effort to write an email validation system in javascript. If you're using html5 which you should be you can just add the required attribute to the input .e.g <input type="email" required> This will flag up on error upon form submission if the email is invalid. For the few users who's browsers do not support this then your server side script should take care of the form validation. You can do the whole javascript thing but it's rather time consuming and the user can just turn off javascript to bypass your script. If you do insist on using javascript email validation then a quick google will get you a good script, they can get very complex, especially for somebody who is new to js so there is totally no point trying to code it yourself. here is an example of one I just plucked from Stack Overlflow: function validateEmail(email) { var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; return re.test(email); } Edit - Just noticed this in your HTML - Do not use tables for layout, they are intended to display tabular data only. research css grid systems if you don't know how to do layouts using css. Edited April 17, 201412 yr by rbrtsmith
Create an account or sign in to comment