Ask Question Forum:
Model Library:2025-02-08 Updated:A.I. model is online for auto reply question page
C
O
M
P
U
T
E
R
2
8
Show
#
ASK
RECENT
←
- Underline
- Bold
- Italic
- Indent
- Step
- Bullet
- Quote
- Cut
- Copy
- Paste
- Table
- Spelling
- Find & Replace
- Undo
- Redo
- Link
- Attach
- Clear
- Code
Below area will not be traslated by Google,you can input code or other languages
Hint:If find spelling error, You need to correct it,1 by 1 or ignore it (code area won't be checked).
X-position of the mouse cursor
Y-position of the mouse cursor
Y-position of the mouse cursor
Testcursor
caretPos
Attachment:===
Asked by Camilla
at 2024-08-12 11:13:37
Point:500 Replies:17 POST_ID:828737USER_ID:11642
Topic:
jQuery;;Programming for ASP.NET
I can do this using ASP.Net's required field validator but I like to try JQuery.
This is the code I have below. I want to display the error msg in "p" section if the textbox is empty. I want to display "please enter a value".
I think I need to do something like this;
This is the code I have below. I want to display the error msg in "p" section if the textbox is empty. I want to display "please enter a value".
I think I need to do something like this;
$('btnSubmit').click (function () { if ($('txtInvitationCode').value == "") $('p') ///now what??}); 1:2:3:4:5:6:
Code I have
<"div class="comments_area">" <"div class="comment_box">" <"div class="up">" <"div class="snap">"<"a href="">"<"img src="../images/gallery_icon1.png" alt="Invitation Code" />"<"/a>" <"/div>" <"div class="upSection">" <"strong>"Please enter Invitee's email address:<"/strong>" <"asp:TextBox ID="txtInvitationCode" CssClass="textbox" runat="server">"<"/asp:TextBox>" <"/div>" <"/div>" <"!-- up ends -->" <" class="down">" <"p>" ( put error msg here) <"/p>" <"asp:Button ID ="btnSubmit" CssClass="reply" Text="Submit" runat="server" />" <"/div>" <"/div>" 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:
Accepted Solution
Expert: julianH replied at 2024-08-12 22:35:45
250 points EXCELLENT
Corrected code
<script type="text/javascript">$(function () { $('#<%= btnSubmit.ClientID%>').click (function (e) { // Get the generated ASP.Net ID for the control FORGOT THE # here if ($('#<%= txtInvitationCode.ClientID%>').value == "") { e.preventDefault(); // Code above refered to a class (.errMSG) instead of an ID $('#errMSG').html('<b>Please enter in a value.</b>'); } });});</script> 1:2:3:4:5:6:7:8:9:10:11:12:
Alternative
<script type="text/javascript">$(function () { // Intercept the form submit rather than the button click - makes a little more sense $('#form1').submit (function () { // Get the generated ASP.Net ID for the control FORGOT THE # here if ($('#<%= txtInvitationCode.ClientID%>').value == "") { // Code above refered to a class (.errMSG) instead of an ID $('#errMSG').html('<b>Please enter in a value.</b>'); return false; } return true; });});</script> 1:2:3:4:5:6:7:8:9:10:11:12:13:14:
Author: Camilla replied at 2024-08-12 17:36:30
yes! RequiredFieldValidator would've been faster but glad I did this. I really wanted to do this with JQuery.
Thanks guys.
Thanks guys.
Author: Camilla replied at 2024-08-12 17:05:46
oh, i missed that. Let me try.
Expert: guru_sami replied at 2024-08-12 16:59:21
Please follow the code snippet in my last post.
Author: Camilla replied at 2024-08-12 16:54:21
That works but the error msg appears and then disappers! I added updatepanel just in case that would solve it but it didnt.
Do I need to add "return false" to the script? I've attached what the page renders to.
Is it <p> tag that messes it up? not sure
Do I need to add "return false" to the script? I've attached what the page renders to.
Is it <p> tag that messes it up? not sure
Author: Camilla replied at 2024-08-12 14:35:26
let me try it again
Expert: julianH replied at 2024-08-12 13:40:14
Raw should be
<script type="text/javascript">$(function () { $('#<%= btnSubmit.ClientID%>').click (function () { // Get the generated ASP.Net ID for the control FORGOT THE # here if ($('#<%= txtInvitationCode.ClientID%>').value == "") { // Code above refered to a class (.errMSG) instead of an ID $('#errMSG').html('<b>Please enter in a value.</b>'); } });});</script> 1:2:3:4:5:6:7:8:9:10:11:
Expert: julianH replied at 2024-08-12 13:39:15
You are missing the '#' here
Should be
Assisted Solution
Expert: guru_sami replied at 2024-08-12 13:14:19
250 points EXCELLENT
It should be: if ($('#<%# txtInvitationCode.ClientID%>').val() == "")
So your js code would look something like:
So your js code would look something like:
$('#<%#btnSubmit.ClientID%>').click(function (e) { if ($('#<%#txtInvitationCode.ClientID%>').val() == "") { $('#errMSG').text("please enter a value"); e.preventDefault(); }}); 1:2:3:4:5:6:
Author: Camilla replied at 2024-08-12 13:07:29
This
But still doesnt display the msg. "test" pops up. But "test2" alert doesnt.
I put some alerts and this comes out as undefined. I tried .val() as well
alert($('<%= txtInvitationCode.ClientID%>').value);
The Script renders as such.
$(function () { $('#ContentPlaceHolder1_btnSubmit').click(function () { alert("test"); // Get the generated ASP.Net ID for the control if ($('ContentPlaceHolder1_txtInvitationCode').val() == "") { alert("test2"); // Code above refered to a class (.errMSG) instead of an ID $('#errMSG').html('<b>Please enter in a value.</b>'); } }); }); 1:2:3:4:5:6:7:8:9:10:11:12:13:
Textbox renders as such
<"input name="ctl00$ContentPlaceHolder1$txtInvitationCode" type="text" id="ContentPlaceHolder1_txtInvitationCode" class="textbox">" 1:
and the p tag like this
Expert: guru_sami replied at 2024-08-12 12:55:25
For ASP.NET controls you need like below:
$('#<%#btnSubmit.ClientID%>').click ....
Same for TB and you need val()
if ($('#<%# txtInvitationCode.ClientID%>').val() == "") ....
$('#<%#btnSubmit.ClientID%>').click ....
Same for TB and you need val()
if ($('#<%# txtInvitationCode.ClientID%>').val() == "") ....
Author: Camilla replied at 2024-08-12 12:54:42
Julian, we posted at the same time. Let me take a look at what you posted.
thanks
thanks
Author: Camilla replied at 2024-08-12 12:52:22
No, it's not being called. This is my button. It has a "runat=server"...is that ok? I do need that
<asp:Button ID="btnSubmit" CssClass="button" style="float:right" Text="Submit" runat="server"></asp:Button> 1:
I've attached how the code renders. Do I have the correct JQuery libraries in there?
Expert: julianH replied at 2024-08-12 12:42:45
to correct the above code
<script type="text/javascript">$(function () { $('btnSubmit').click (function () { // Get the generated ASP.Net ID for the control if ($('<%= txtInvitationCode.ClientID%>').value == "") { // Code above refered to a class (.errMSG) instead of an ID $('#errMSG').html('<b>Please enter in a value.</b>'); } });});</script> 1:2:3:4:5:6:7:8:9:10:11:
<div class="comments_area"> <div class="comment_box"> <div class="up"> <div class="snap"><a href=""><img src="../images/gallery_icon1.png" alt="Invitation Code" /></a> </div> <div class="upSection"> <strong>Please enter Invitee's email address:</strong> <asp:TextBox ID="txtInvitationCode" name="txtInvitationCode" CssClass="textbox" runat="server"></asp:TextBox> </div> </div> <!-- up ends --> < class="down"> <p id="errMsg"> ( put error msg here) </p> <asp:Button ID ="btnSubmit" CssClass="reply" Text="Submit" runat="server" /> </div> </div> 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:
Expert: Big Monty replied at 2024-08-12 11:58:49
you can do something like this:
<p id="errMSG"></p>$('btnSubmit').click (function () { if ($('txtInvitationCode').value == "") $('.errMSG').html('<b>Please enter in a value.</b>');}); 1:2:3:4:5:6:7:8:
i put html in the code just to demonstrate you can style it anyway you like.
Author: Camilla replied at 2024-08-12 11:58:39
no, I dont want to popup a msg box. I want it inline, inside that <p> tag.
If it's not doable, i'll just do it in requiredfieldvalidator.
If it's not doable, i'll just do it in requiredfieldvalidator.
Expert: duncanb7 replied at 2024-08-12 11:18:09