html5 input pattern attribute not working outside a form? - html

this fiddle works as intended - it displays a warning when the user enters an invalid country code.
This other fiddle, without the form element, doesn't work. It seems the input's pattern attribute needs a form to validate. By the way, I'm doing a complex page without forms and I' d like to validate my input fields with `pattern. Is there a way to do that?

This is because the validation is part of the HTML5 form validation (http://www.w3.org/TR/html5/forms.html#client-side-form-validation). The validations are triggered when the form is submitted en when there are any errors, the submit will be cancelled.
To trigger it manually, use the checkValidity() function on the element:
$('input').blur(function(evt) {
evt.target.checkValidity();
}).bind('invalid', function(event) {
alert('oops');
});
http://jsfiddle.net/y66vH/3/

Validation is done at <form> submission time. If you want to use the browser's natural form validation and its corresponding UI, you need to use a <form> with a corresponding submit input to allow the user to submit the form naturally.
However, validation is triggered before the submission event is triggered. Therefore, you can prevent the default form submission behavior while still using the browser's own validation.
document.querySelector("form").addEventListener("submit", function (e) {
e.preventDefault();
});
http://jsfiddle.net/ExplosionPIlls/2gaw3/1/

The element must be part of the form. If that is not possible, just add form="formID" to your "outside" element.
HTML
<form id="form1" action="demo_form.asp">
<input type="submit" />
</form>
Country code: <input type="text" name="country_code" pattern="[A-Za-z]{3}" form="form1" title="Three letter country code" />
js fiddle: http://jsfiddle.net/y66vH/1/

The key points are:
Yes, validation works with ajax forms.
Don't use an onclick handler. Use onsubmit on a wrapping form and have your button be type="submit" under that form.
In your submit handler, use a pattern like if (!theFormOrInput.checkValidity()) return; right away.
You must call event.preventDefault() after the validity check or else the browser popover won't display. Validation is part of the default action; if you cancel too early you're unintentionally opting out.

Related

How can I prevent an html form from redirecting when it's still incomplete?

So I'm coding exclusively in HTML and CSS for this project. I have certain parts of my form marked as required. At the bottom, the submit button looks like this:
<input type="submit" value="submit" onClick="location.href='thankyou.html'">
When I click submit, even if the required spaces are blank, it still directs to that page which says it's been submitted succesfully. I want it to stay on that page and show that the form is incomplete and only successfully submit when the required spots are filled.
Using javascript, replace the click function and validate the required fields have data. If not call the event object method preventDefault() (or if you are using jQuery return false).
An example:
$('.form').unbind('click').click(function (e) {
if (!check_form()) {
e.preventDefault(); //This could also be return false as I'm using jQuery.
}
}
The jQuery documentation is here: http://api.jquery.com/event.preventdefault/
And the MDN documentation for preventDefault here: https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault
You can use the onsubmit handler of the form element for this. If you return false from the handler, the form won't submit, if you return true, it does:
<form action="some-action.html" method="post" onsubmit="return checkIfFieldsAreEmpty()">
<input type="text" name="blah">
<input type="submit" value="Submit">
</form>
<script>
function checkIfFieldsAreEmpty(){ /* logic to check fields */ }
</script>
Important: the way you set up an onclick event handler on the submit button will actually prevent your form from submitting completely (it just loads a new page with the form never submitting; so remove that event handler.
I leave it as an exercise to you to implement the actual checking logic in the function as I don't have access to your HTML.

How to design form tags button in html

I have a button that is this <button id="btnSubmit">Submit</button> the problem is, I want the form tags to use this id so that is designed the way I want. And also this code, I have a few questions.
<form action="demo_form.php" method="get">
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
<input type="submit" value="Submit">
</form>
what is the action="demo_form.php",input type="submit" do? And does the input type has any other "What do you call this stuff" besides submit?
The action="demo_form.php" sets the action, in this case, "navigate to file demo_form.php and send it the data".
<input type="submit" (...) > creates and element which submits the form e.g. executes the "action".
The method sets the way the data is submitted to the target of the action ("form_demo.php"), in this case get, which allows you to refer to the submitted data as $GET["name"] in PHP.
Possible input types are listed here.
You either give your <input type="submit" (...) > the id="btnSubmit" property or use javascript to submit the form after an event has been triggered.
MOr info on that is available here (i short: document.<get_the_form_element>.submit();).
I suggest you to take a look at this link. It describes all the basic concepts about how using forms. And you can also find a lot of information by Googling it.
The action attribute
The action attribute defines the action to be performed when the form is submitted.
The common way to submit a form to a server, is by using a submit button.
The input attribute
<input type="submit"> defines a button for submitting a form to a form-handler.
The form-handler is typically a server page with a script for processing input data.
If i understand your question correctly, these are my answers.
action
The action attribute describes the page to which the contents of the form are sent to. So if you have a sign up form with an input for an email, the text that is typed will be sent to the action path. It will be sent using the method described in the method attribute. So you can find your values in either the $_POST variable, or the $_GET variable, get is easy for being able to share the url and post is great for private information.
input
The input element is the actual way to input information (who guessed it). You've got an input of the type text for just text input, you've got checkbox for a true or false input and way way more see: w3schools
why don't you use
<input type="submit" value="Submit" id="btnSubmit">
Or if you want to use a button
<button id="btnSubmit">Submit</button>
Then from jquery or js you can submit the form.
And for this question,
what is the action="demo_form.php",input type="submit" do?
You should probably google it out. This is so basic.
Anyway, just a concise explanation:
action is the attribute where you will specify the code that will handle the form data submitted and input type="submit" will display a button in the page, clicking on it will submit the form.
There are a lot of types in input, the most common ones are
text
password
submit

How to programmatically display HTML5 client-side form validation error bubbles?

I'm trying to use HTML5 client-side validation (i.e. inside a form), but cannot see how to display the validation error bubbles programatically.
Consider the following:
<form id="myForm">
<input type="text" id="number" min="1" max="10" step="3" required>
</form>
If there is a canonical submit button (i.e <input type="submit">), and there are validation errors, the user-agent will halt the submit and show UI to the user:
But, if instead of a using a submit input, the user is clicking an anchor that executes javascript (i.e. ASP.net Webforms):
<a href='javascript:SaveChanges()'>Save Quantity</a>
<script>
function SaveChanges()
{
var form = document.getElementById('myForm');
if (form === null) return;
if (!form.checkValidity())
{
//We reach here, but no UI is displayed
return;
}
form.submit();
</script>
The issue is that while
form.checkValidity();
does check the form's validity (returning false if it's not valid), it does not trigger the UI displays.
And now we have our question. Submitting through
<input type="submit"> works (halts and shows UI)
<button type="submit> works (halts and shows UI)
form.submit doesn't work (doesn't halt; doesn't show UI)
form.checkValidity() doesn't work (doesn't show UI)
How to programmatically display HTML5 client-side form validation error bubbles?
jsFiddle for all of the above
See also
How to programmatically display HTML5 client-side validation error bubbles?
Trigger standard HTML5 validation (form) without using submit button?
Triggering HTML5 Form Validation
This might not be the cleanest way, but I added a hidden submit button and programmatically trigger the click event on it.
I updated the jsfiddle this way:
Add a new submit button #fakeButton with a css class:
.fakeButton{
display:none;
}
Add a new link triggering it through a javascript function:
function DoFakeButtonClick()
{
var button = $('#fakeButton');
if (button === null)
return;
button.click();
}
It works pretty well, i think it is currently the only way to do this programmatically

Is it possible to check required input with the HTML required attribute without actually submitting the form?

I have a submit button with an onclick event:
<input type='submit' name='add-row' onclick='addRow()' value='+'>
My input text boxes all have the required attribute. I'd like to prevent the onclick event if the required fields do not hold data, but it looks like the required fields are only checked on submit. I do not want to submit though because that causes the page to reload (I'll have another button for submitting all the forms at once later on).
Is there a way to force the check on required fields sooner or do I have to check each field manually in my onclick function?
The OnClick event handler needs to run script code that checks the individual form fields for data, and then returns true only if the data is acceptable, otherwise returns false to abort the submission.
Update: rather than using an OnClick event handler on the submit button, you should instead use an OnSubmit event handler on the form itself. That way, having the validation script return true/false has more meaning.
Take a look at the HTML5 pattern attribute. More info at http://www.w3schools.com/tags/att_input_pattern.asp
It prevents the form from being submited if a regex pattern is not match by the input field.
Try the following :
Aim:
Use Required Attributes of the input to validate.
Dont allow the form to submit via get or post method but want to perform some dom manipulations using javascript programmatically.
The reason i am trying to do all this is because my form is in a modal and on submit, i need to do few validations including that of checking whether a group of checkboxes have been checked or not.
We can use onsubmit="function()" and action attributes of the form in a different manner as follows:
<form onsubmit = "someFunc()" action="javascript:void(0)">
<input ......... required>
<input type="submit" value="submit">
What the above code does is allows you to validate input fields within your form having the required attribute when you click submit. . Also the form wont get submitted as action is set to javascript:void(0)
Now you can perform the required actions like validations inside the function sumfunct .
for eg, i had to check whether a group of checkbox have been checked or not.
So i used the following code .
if($('div.checkbox-group.required :checkbox:checked').length > 0){
return true; } else { return false;}
Hope This helps!

Difference between HTML forms submit tags

as far as I know there are three methods to submit an HTML form without JS or keyboard:
1. input type=submit
2. button tag
3. input type=image
Is there another way to create an element submitting the form on being pressed?
Is it correct to handle button tag (type=submit) same as input type=submit (I mean, if we discard the fact button can contain inner html, we can simply replace button with input type="submit" and the form will be sent correctly)?
Is adding name.x and name.y the only difference when using input type=image?
Not that I know of, those should be the only pure html ways to submit the form other than directly invoking the submit method which is internal Javascript, but that is what the submit button does anyway.
The button element has issues in Internet Explorer regarding which
value it passes, I do not recommend
the use of it.
Yes, they're pretty much the same
As far as I know input type=image is exactly the same except that it
sends those extra coordinate
parameters which you can ignore on the
server-side.
With JavaScript, you can call the form's submit method. However, this should be avoided as it will not work if the user has JavaScript disabled.
No, because a button tag can be given a value separate from the text displayed on the button, not to mention the <button> tag's ability to inline HTML. (For example <button type="submit"><img src="submit.png" alt="Submit"></button>).
Yes, the main feature of <input type="image"> is the addition of the x and y coordinates.
You can use JavaScript to simulate it. I'd take an <input type="submit"> and replace it with the element that you'd like to submit a form with, so it's still accessible for users without JavaScript.
<input type="submit" id="submit-button" value="submit" />
Then in JavaScript (using jQuery in this example):
$('#submit-button').remove().after('Submit form');
$('#submit-link').click( function(event){
event.preventDefault();
$('#submit-link').closest('form').submit();
});