How to set default button in ASP.Net MVC - html

This is my HTML code :
#using (Html.BeginForm("Index", "EmployeeList", FormMethod.Post, new {defaultbutton = "searchBtn" }))
{
#Html.TextBox("SearchString", ViewBag.SearchString as string, new { #id = "SearchString", #class = "form-control", placeholder = "Search Name" })
<button type="submit" name="MyButton" value="Export" class="btn btn-sm btn-danger">
<button type="submit" name="MyButton" value="Search" id="searchBtn" class="btn btn-sm btn-warning">
}
According to my page design I have to write "Export" button's html code before "Search" button.
While I press enter after filling search text 'export' button gets clicked, instead of that I need 'Search' button clicked.

You can handle kewdown event :
<script type="text/javascript">
$(document).bind('keydown', function (e) {
if (e.which === 13) { // return
$('#searchBtn').trigger('click');
}
});
</script>

If you'd prefer to have a defaultbutton on any form, it's easy enough to add one. Below is a trivial example. Note that I required the defaultbutton to be a selector rather than an id, but it's easy enough to require prefix a hash if you prefer it to accept ids.
Also note that using the name "defaultbutton" runs the risk of conflicting with someone else's code (and the lack of a data- prefix means it might conflict with a possible "defaultbutton" feature added to a future html spec).
Below is a simple example. Changing closest to check for a defaultbutton attribute will allow a single form to have different default buttons for different components.
//Load this script on any pages that need to support default buttons.
$(document).bind('keydown', function(e) {
if (e.which === 13) {
var button = $(e.target);
if (button.attr('type') != 'text') {
return true;
}
var parentform = $(e.target).closest('form');
var selector = parentform.attr('defaultbutton');
var targetbutton = parentform.find(selector);
if(targetbutton.length > 0) {
targetbutton.trigger('click');
return false;
}
return true;
}
});
<!-- Example usage. Just add a defaultbutton attribute to your form-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form defaultbutton='#candy'>
<input type='text' id='text' placeholder='text' /> <br /><br />
<input type='submit' value='toys' id='toys' onclick='alert("Toys") ;return false;' />
<br /><br />
<input type='submit' value='candy' id='candy' onclick='alert("Candy");return false;' />
<br /><br />
<input type='submit' value='money' id='money' onclick='alert("Money");return false;' />
<br /><br />
</form>

Related

How to map the Value attribute of html submit element to asp button element?

HTML element:
<input type="submit" class="searchbox-submit" value="GO">
Asp:
<button runat="server" id="btnRun" title="Search" CssClass="searchbox-submit" onserverclick="btn_Click"></button>
It is necessary by printing text in a textbox, the button from the search icons
accepted value "GO"
TextBox:
<asp:TextBox ID="txtsearch" runat="server" Placeholder="Search..." onkeyup="buttonUp()" CssClass="searchbox-input"></asp:TextBox>
Script:
<script>
function buttonUp() {
var inputVal = $('.searchbox-input').val();
inputVal = $.trim(inputVal).length;
if (inputVal !== 0) {
$('.searchbox-icon').css('display', 'none');
} else {
$('.searchbox-input').val('');
$('.searchbox-icon').css('display', 'block');
}
}
</script>
If I did not express my speech well, please tell me, I will try to correct it.

Which input element that accept digits only and allows a limited number of characters

I am trying to implement an (zip code of my country) input field that only accept digits and limit the number of characters to 5.
Which html input type and attributes are more appropriate for this task ?
<input type="text" name="zipcode" pattern="[0-9]{5}" />
pattern="\d{5}" would also work. But make sure to re-check it in your target-script because form-elements can be manipulated.
A complete example (WITH JQUERY) for checking values on change or keyup in the .checkFieldNum values and on submit the form.
JS
function checkNum(value)
{
return value.match(/\d{5}/);
}
$(document).ready(function(){
// trigger if element-value with class="checkFieldNum" changes or key is pressed
$('.checkFieldNum').on('change keyup',function(event){
if(checkNum($(this).val()) === false)
{
$(this).css({background:'tomato'})
}
else
{
$(this).css({background:''})
}
})
// trigger if form should be submitted
$('.formClassForSubmit').on('submit',function(event){
event.preventDefault();
var goOn = true;
$('.checkFieldNum').each(function(){
if(checkNum($(this).val()) === false)
{
goOn = false;
alert($(this).attr('name') + ' has wrong value')
}
})
if(goOn === true)
{
// In this case, everything is OK and go on whatever you will do..
}
})
})
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form action="?" method="post" class="formClassForSubmit">
<input type="text" class="checkFieldNum" name="zipcode1" pattern="[0-9]{5}" />
<input type="text" class="checkFieldNum" name="zipcode2" pattern="\d{5}" />
<input type="submit" name="send" value="send" />
</form>
see https://jsfiddle.net/7hbj6a3e/3/
<input type="number" minlength="5" maxlength="5" name="zipcode" />
see https://www.w3schools.com/tags/tag_input.asp

Login button is not working while pressing enter in a pop up window form in chrome extension

Hi I have created a pop up window containing a form asking the user to enter email and password.And there is a log in button.Usually in most of the forms, after entering all details and pressing 'enter', the submit button will be automatically clicked.But in this case,only when I click the login button it works.I want it to work on pressing 'enter' in keyboard.I know that this is a simple question.But I can't figure it out why is this happening.Please help me
Here is my userinfo.html(pop up window)
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="test.js"></script>
</head>
<body>
<b>Enter your Email ID and Password</b><br><br>
<form id="userinfo">
<label for="user"> Email : </label>
<input type="text" id="user" /><span id="semail"></span>
<br><br>
<label for="pass">Password : </label>
<input type="password" id="pass" />
<br>
<br>
<input type="button" id="login" value="Log In"/>
</form>
</body>
</html>
Here is my test.js
window.addEventListener('DOMContentLoaded', function() {
var user = document.querySelector('input#user');
var pwd = document.querySelector('input#pass');
var login = document.querySelector('input#login');
login.addEventListener('click', function() {
var userStr = user.value;
var pwdStr = pwd.value;
chrome.runtime.getBackgroundPage(function(bgPage) {
bgPage.login(userStr,pwdStr); });
window.close();
});
});
In order for a submit event to be triggered on ENTER the form must have a submit button.
In your particular case, you need to revent the default behaviour of your form, since you want to handle it "manually" using the background-page.
In summary, the following steps are required:
Change the button type from button to submit.
Register a listener for the form's submit event. (Note: this will be triggered on both button-click and ENTER.)
Prevent the default behaviour on form-submit.
Delegate the login-handling to the background-page (through its login method.
Here is the new code:
test.html
<h4>Enter your Email ID and Password</h4>
<form id="userinfo">
<label for="user">E-mail: </label>
<input type="text" id="user" required />
<br />
<label for="pass">Password: </label>
<input type="password" id="pass" required />
<br />
<br />
<input type="submit" id="login" value="Log In" />
</form>
test.js
window.addEventListener('DOMContentLoaded', function () {
var form = document.querySelector('form#userinfo');
var user = document.querySelector('input#user');
var pass = document.querySelector('input#pass');
/* Register a listener on the form's `submit` event */
form.addEventListener('submit', function (evt) {
/* Prevent the event from being handled by the browser */
evt.preventDefault();
var userStr = user.value;
var passStr = pass.value;
/* Delegate login-handling to background-page */
chrome.runtime.getBackgroundPage(function (bgPage) {
bgPage.login(userStr, passStr);
});
window.close();
});
});
See, also, this short demo.

Having two submit buttons [duplicate]

This question already has answers here:
Multiple submit buttons in an HTML form
(27 answers)
Closed 9 years ago.
I have two submit buttons, a back submit button and a next submit button, when the user is in a text input and press enter, it takes them backwards... I think this is because enter evokes the back submit button instead of the next submit button.
<form action="" method="post">
<input type="submit" name="GOTO1" value="back" />
<input type="text" name="value1" />
<input type="submit" name="GOTO3" value="next" />
</form>
So when you are instead of the text field, and press enter it executes the first submit button, how can I change that...
Go the jQuery route...(untested).
// Prevent default on form on page load
// or on "enter"
$('form').disable();
// OR....
// Disable the ENTER key altogether on the form inputs
$('form').find('.input').keypress(function(e){
if (e.which == 13) // Enter key is keycode 13
{
return false;
}
});
$('input[type="submit"]').on('click', function() {
var btn = $(this).val();
if(btn == 'back')
{
// do this to go back, code here
}
else
{
// do this to go to next, code here
}
return false;
});
very simple workaround
<script>
function myFunction()
{
}
function myFunction2()
{
}
</script>
</head>
<body>
<form action="" method="post">
<input type="submit" name="GOTO1" value="back" onclick="myFunction2()"/>
<input type="text" name="value1" />
<input type="submit" name="GOTO3" value="next" onclick="myFunction()" />
</form>
Not neat but it works on the onclick number reference.
Hope this helps...
Use
<input type="button" ... >
You should not have more than one submit per form, since ENTER should trigger it.
To know more about it, you can take a look at this: https://stackoverflow.com/a/469084/955143

HTML help --hidding a button

I need help with my HTML styling.
here is the HTML code:
<div id="hidden" style="display: none;">
<label>URL:</label>
<input type="text" name="url" size="50"/><br/>
<input type="button" id="button2" value="Update"/> <br/>
</div>
<input type="button" id="button1" value ="Get Info"
onclick="document.getElementById('hidden').style.display = '';" size="25"/>
As you can see, all the elements that are inside <div></div> will be displayed upon clicking button1 (they are hidden initially).
What I want is that when button1 is clicked in addition to all the other fields (including button2) being displayed, button1 to be hidden.
How can I do this?
Change
onclick="document.getElementById('hidden').style.display = '';"
to
onclick="document.getElementById('hidden').style.display = ''; this.style.display = 'none'"
You can see this in action here: http://jsfiddle.net/nayish/sJ5RR/.
instead of '' change it to 'Block'
so:
<input type="button" id="button1" value ="Get Info"
onclick="document.getElementById('hidden').style.display = 'Block';" size="25"/>
But the best way will be to externalize your script so you can do more stuff without cluttering your html
var btn = document.getElementById('button1');
btn.onclick = function () {
// all the stuff you want to do
document.getElementById('hidden').style.display = "block";
this.style.display = "none";
... more stuff ...
}