how to compare radio button values with textbox values in asp.net - asp.net

i have four radio buttons and one text box..i have to check the selected radio button value equals to the textbox value.. anyone plz help me

if(radioButtonList.SelectedValue == textBox1.Text.Trim())
{
//your code goes here
}

textBox does not contain a Value property.
if (!string.IsNullOrEmpty(RadioButtonList1.SelectedValue) &&
RadioButtonList1.SelectedValue.Equals(TextBox1.Text, StringComparison.Ordinal))
{
//your code goes here
}

Well.You didn't clarify where you want to do this comparison i.e. ClientSide or ServerSide.If you want it server side you can prefer earlier posted answers otherwise for client side try this one using Jquery.
<div>
<input type='radio' name='rd' value='A'>
<input type='radio' name='rd' value='B'>
<input type='radio' name='rd' value='C'>
<br />
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</div>
<script type="text/javascript" >
$(document).ready(function(){
$("input:radio[name='rd']").click(function(){
if($(this).is(":checked"))
{
if($.trim($(this).val()) == $.trim($("#txtName").val()))
alert("Yeah!I got matched value.");
else
alert("Oops!Not matched.");
}
});
});
</script>
Click on this link:
DEMO

Related

How to set required field in contact form 7 wordpress

I have a list of multiple checkboxes in the form. The user can check all the checkboxes but at least one is required. Without selecting at least one, checkbox they should not be able to submit the form. how to make it in contact form 7?
I have added the script which is not working.
Here is my script
$(document).ready(function () {
$('#checkBtn').click(function() {
checked = $("input[type=checkbox]:checked").length;
if(!checked) {
alert("You must check at least one checkbox.");
return false;
}
});
});
In this only write star(*) after checkbox element in the plugin form
[checkbox* checkbox "test 1" "test 2"]
[submit "Send"]
It is perfectly work.
On this set your code
$('#fm_submit').submit(function(e){
e.preventDefault();
var ck_box = $('input[type="checkbox"]:checked').length;
// return in firefox or chrome console
// the number of checkbox checked
console.log(ck_box);
if(ck_box < 1){
alert('Select any one box');
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name = "frmTest[]" id="fm_submit">
<input type="checkbox" value="true" checked="true" >
<input type="checkbox" value="true" checked="true" >
<input type="checkbox" >
<input type="checkbox" >
<input type="submit" id="fm_submit" name="fm_submit" value="Submit">
</form>
<div class="container"></div>
Paste the code from above on the bottom of the form instead of the Additional Settings
$('#fm_submit').submit(function(e){
e.preventDefault();
var ck_box = $('input[type="checkbox"]:checked').length;
// return in firefox or chrome console the number of checkbox checked enter code here
console.log(ck_box);
if(ck_box < 1){ alert('Select any one box'); }
});

How can I check in asp.net if checkbox was chcked?

How can I check in asp.net if checkbox was chcked?
Here the CheckBox:
<input type="checkbox" id="FootBallManager2013CheckBox" />
Since your markup is not using a CheckBox server control, I'll assume you want to check the input manually.
You have to give the input a name, otherwise it can't be posted. Call it, for example, name="FootBallManager2013CheckBox".
In the code-behind, look in Request.Form["FootBallManager2013CheckBox"] and see if it is non-blank. If the form element is present, it was checked; if it's not, it wasn't.
Example:
var footBallManager2013CheckBoxChecked = !string.IsNullOrEmpty(Request.Form["FootBallManager2013CheckBox"]);
Or, you could just use the server control, which is easier.
<asp:CheckBox runat="server" id="FootBallManager2013CheckBox" />
Code-behind:
if (FootBallManager2013CheckBox.Checked)
{
}
change html as
<input type="checkbox" id="FootBallManager2013CheckBox" RunAt="Server"/>
then on server side in code behind
if(FootBallManager2013CheckBox.Checked)
{
}
if(FootBallManager2013CheckBox.Checked)
{
}
you can tell by the checked property on the checkbox:
input type="checkbox" id="FootBallManager2013CheckBox" checked="checked"
or code wise (vb)
IF FootBallManager2013CheckBox.checked = checked then
[write what you want to do]
End IF

Why am I having trouble selecting a default radio button on a web page?

It seems this ought to be dead simple, but I'm stuck. I've written some asp.net code that outputs a pair of radio buttons:
<p>
<label for='chkYapper'>Yapper</label>
<input type='radio' name='yapper' id='chkYapper' value='yapper' checked='<%=gblYapperChecked %>' />
<br />
<label for='chkNonYapper'>non-Yapper</label>
<input type='radio' name='Yapper' id='chkNonYapper' value='nonYapper' checked='<%=gblNonYapperChecked %>' />
if (registrationUser.isYapper == 1)
{
gblYapperChecked = "checked";
gblNonYapperChecked = "";
}
else
{
gblYapperChecked = "";
gblNonYapperChecked = "checked";
}
As expected, I get two radio buttons, "Yapper" and "Non-Yapper". However, even when I step thru my code and see that gblYapperChecked is "checked" and gblNonYapperChecked is "", Non-Yapper is always selected by default in the web browser.
What am I doing wrong?
UpdateHere is the HTML code as it actually appears in the browser. "Yapper" should be selected, but "Non-Yapper" appears selected instead.
<p>
<label for='chkYapper'>Yapper</label>
<input type='radio' name='yapper' id='chkYapper' value='yapper' checked='checked' />
<br />
<label for='chkNonYapper'>non-Yapper</label>
<input type='radio' name='yapper' id='chkNonYapper' value='nonYapper' checked='' />
Note that the HTML "checked" attribute is generally determined by being present or not present. See http://www.w3.org/TR/html401/interact/forms.html#adef-checked for the spec.
In particular what this means is that if you want it to be checked you cna have checked, checked=true, checked=checked and so on. So what you want is to not have the checked attribute at all if you don't want the checkbox selected.
I would advise structure such as:
<input type='radio' name='Yapper' id='chkNonYapper' value='nonYapper' <%=registrationUser.isYapper?"":"checked='checked'" %> />
This should eliminate your checked attribute entirely dependant on your isYapper boolean.
The "checked" attribute is weird, it has no value. If a radio button is checked, include the "checked" attribute by itself in the tag. If unchecked, don't do anything. See here:
http://www.htmlcodetutorial.com/forms/_INPUT_TYPE_RADIO.html
Are you setting dblYapperChecked before or after the control is created? Personally, I'd run the radio buttons on the server side and set the checked value on the control directly, but your method should work if the values are set soon enough (try initializing them to the expected values and see if that makes a difference...)

jquery next button focus

To improve navigation on one of the pages I am tyring to set a focus on a next available(enabled) button when leaving last data entry field.
$('input[type=text], select, textarea').filter(':last').blur(function()
{
$('input[type=submit][type=button]:enabled:first').focus();
});
For some reason it only works when last data entry field is textbox. Something is wrong in the handler.
$(document).ready(function() {
$(':text,textarea,select').filter(':last').blur(function()
{
$(':button,submit:enabled:first').focus();
});
});
<body>
<textarea rows="3" /></textarea>
<select>
<option>1</option>
<option>2</option>
</select>
<input type="text" />
<input type="button" value="Something" />
</body>
Did the trick ... pretty much identical, so I don't know what's not working for you.
$('input').filter(':last').blur(function()
{
$('input:enabled:first').focus();
});
doesn't do the trick?
Assign the buttons a CSS class and try $('.ButtonClass:enabled:first').focus();

jQuery to get the text attribute of a checkbox

I'm adding a check box to a page using the following statement;
<script language="C#" runat="server">
protected void Page_Load ( object src, EventArgs e )
{
if (!IsPostBack)
{
CheckBox XChkBox = new CheckBox(); //instance of System.Web.UI.WebControls.CheckBox
XChkBox.ID = "someId"
XChkBox.Text = "someText"
somePlaceHolder.Controls.Add(XChkBox);
}
}
</script>
I need to get the Text attribute of that check box on click. I tried $(this).attr('Text'); inside $('input[type=checkbox]').click(function(){}); but it returns undefined.
Where am I going wrong? Please suggest.
cheers
ASP .NET renders the Text property of the ASP:CheckBox server control, as a label element just after the <input type="checkbox" /> on the generated markup at the client, it looks something like this:
<input id="someId" type="checkbox" name="someId" />
<label for="someId"> someText </label>
You can get the text of the label by:
$("input:checkbox").click(function() {
var $label = $(this).next('label');
alert($label.text());
});
The CheckBox control renders the Text inside a <label> element. The text is not part of the HTML checkbox. If you want to get the text from jQuery, you have to get it from the <label>.
Also, the <label> it generates doesn't actually have an ID. If your CheckBox is named checkBox1, then the HTML it outputs will be <label for="CheckBox1">, and the text is inside that element. I believe the correct jQuery syntax would be:
$('label[for="checkBox1"]').html()
This depends on how you're implementing what you call "text".
If it's like this:
<input id="chkFoo" type="checkbox" text="Check me, fool!" />
Then you can access the text like this:
$("#chkFoo").attr("text")
If you do it like this
<input id="chkFoo" type="checkbox" />Check me, fool!
I think you're out of luck. Put a span around the text if you have to do it this way and grab it like this:
<input id="chkFoo" type="checkbox" /><span id="spnFoo">Check me, fool!</span>
$("#spnFoo").text()

Resources