html how do I make required fields in forms [duplicate] - html

This question already has answers here:
How do I make a field required in HTML?
(16 answers)
Closed 2 months ago.
Is there any way i could make fields required. I tried the using required="required" in the input tag, but it still did not work. is there any other way?
<input name="Forename" type="text" required="required" id="Forename2" onkeyup="allLetter(this)"/>

you can add required to your input field, so it can be :
<input name="Forename" type="text" id="Forename2" onkeyup="allLetter(this)" required />
Hope this will help you :)

The required attribute can simply be included like this:
<input required>
So in your case, remove the attribute tag:
<input name="Forename" type="text" required id="Forename2" onkeyup="allLetter(this)"/>

In order to make required attribute work you should wrap your inputs into <form> tag. After that onSubmit event will give you behavior you expected.

You can try something like this:
function validate() {
var value = document.getElementById("myfield").value;
//make what you want
console.log(value);
}
<form >
<label>
your field: <input type="text" id="myfield" name="my_field" required onkeyup="validate()">
<input type="submit">
</label>
</form>

Related

HTML form input readonly

i'm currently using readonly for html inputs,.. but in some of html version (I think so) it can be editable,.. is there any other option instead of readonly to make it noneditable?
my input tag is
<input type = "text" id = "plan_amount" readonly>
`
Thanks in advance
You can use disabled attribute: <input disabled>.
And you can add an <input type="hidden"> if you want to POST the value.

I want to concatenate two text fields in html and display the result in other test field [duplicate]

This question already has answers here:
Concatenate multiple HTML text inputs with stored variable
(2 answers)
Closed 9 years ago.
I have my code like this
First name : <input type="text" name="txtFirstName" /> <br><br>
Last name : <input type="text" name="txtLastName" /> <br><br>
Full name : <input type="text" name="txtFullName" > <br><br>
if i give abc in first name text box and def in last name text box the result should be displayed as abcdef in full name text box. How to do this?
It's actually quite simple with a tiny bit of inline JavaScript using the form oninput attribute.
<form oninput="txtFullName.value = txtFirstName.value +' '+ txtLastName.value">
First name : <input type="text" name="txtFirstName" /> <br><br>
Last name : <input type="text" name="txtLastName" /> <br><br>
Full name : <input type="text" name="txtFullName" > <br><br>
</form>
http://jsfiddle.net/RXTV7/1/
I'd also suggest using HTML5 <output> element instead of third input. To learn more start here: http://html5doctor.com/the-output-element/
Bind a function that generates the full name on keyup events for your inputs...
<script type="text/javascript">
function generateFullName()
{
document.getElementById('fullName').innerText =
document.getElementById('fName').value + ' ' +
document.getElementById('lName').value;
}
</script>
First Name <input type="text" id="fName" onkeyup="generateFullName()" /><br/>
Last Name <input type="text" id="lName" onkeyup="generateFullName()" /><br/>
Full Name <span id="fullName" />
if you want, you can have the FullName as a input too, and set it's Value.
Try this (using jQuery). it will work. But the fullname field will remain empty if the individual fields are empty
<script>
$(document).ready(function(){
$("fullName").focus(function(){
var fullname = $("fName").val() + $("lName").val();
$("fullName").val(fullname);
});
});
</script>
First Name <input type="text" id="fName" /><br/>
Last Name <input type="text" id="lName" /><br/>
Full Name <span id="fullName"/>
For manipulating HTML you'll need to use JavaScript. There are tons of good tutorials out there, for example on w3schools.com.
You may also want to check out jQuery, which makes this kind of manipulations a lot easier and more straightforward.
you can use the below code for that:
<script type="text/javascript">
function generateFullName()
{
document.getElementById('txtFullName').value =
document.getElementById('fName').value + ' ' +
document.getElementById('lName').value;
}
</script>
First Name <input type="text" id="fName" /><br/>
Last Name <input type="text" id="lName" oninput="generateFullName()" /><br/>
Full name <input type="text" id="txtFullName" name="txtFullName" > <br><br>
Also, instead of oninput event , you can opt for onblur also.

Showing required fields by bolding

I have an HTML form. I would like to show that some of the fields are required by making them bold. In principle, should this go into the CSS rather than HTML? How would you do it?
<form action="doit" id="doit" method="post">
<label>
Name
<input id="name" name="name" type="text" />
</label>
<label>
Phone number
<input id="phone" name="phone" type="text" />
</label>
<label>
Year
<input id="year" name="year" type="text" />
</label>
</form>
Just of the top of my head, I think that if you're willing to use HTML5 and use the <input type="text" name="year" required> property, that you should be able to do:
input:required{
font-weight:bold;
}
And of course, you could go wild here and start throwing around borders and all sorts of stuff to make it really stand out.
Singularity's answer is perfectly valid. For the sake of completion, if you're not willing to use HTML5's required attribute, I would recommend adding a class by the same name to the inputs that are required.
<input type="text" name="firstname" class="required">
input.required {
font-weight: bold;
}
You can further use that class as a selector in your Javascript where you enforce the rule.
$(form).submit(function() {
$('input.required').each(function() {
if ($(this).val() === '') return false;
});
});
To answer the other question you were asking: the bold directive should go in CSS since it is purely presentational.

How do I edit the HTML form elements when in CSS? Do I assign an id?

<form id="form">
Name: <input type="text" name="name"/></br>
Status Message: <input type="text" name="statusmessage"/><br/>
</form>
So, do I assign an id="name" within the brackets? And then in CSS, do I call it with #form name?
html:
<input type="text" name="name" id="id" class="class" />
css: (any of the following would style the element)
input [name=name]{}
#id{}
.class{}
or you could style all text input fields in the form with:
#form input[type=text]{}
If you are using CSS2 you can use <input type="text" name="name" id="name" /> and then write this CSS:
#name {
:
}
It will select the element with ID name. Remember that using additional address info is unnecessary since the ID is uniquely sufficient to select any single element.

HTML input field hint

I want to provide the user with a hint on what he needs to enter into my text field. However, when I set the value, it does not disappear once a user clicks on the text field. How can you make it disappear?
<form action="input_password.htm">
<p>Username:<br><input name="Username" value="Enter username.." type="text" size="20" maxlength="20"></p>
</form>
With a bit of JavaScript:
<input
value="Enter username..."
onfocus="if (this.value === 'Enter username...') this.value=''" ... />
HTML5 has a nice attribute for this, called placeholder:
<input placeholder="Enter username.." ... />
but this attribute is not supported in old browsers.
the best way to give a hint is placeholder like this:
<input.... placeholder="hint".../>
You'd need attach an onFocus event to the input field via Javascript:
<input type="text" onfocus="this.value=''" value="..." ... />
I think for your situation, the easy and simple for your html input , you can
probably add the attribute title
<input name="Username" value="Enter username.." type="text" size="20" maxlength="20" title="enter username">
With HTML5, you can now use the placeholder attribute like this:
<form action="demo_form.asp">
<input type="text" name="fname" placeholder="First name"><br>
<input type="text" name="lname" placeholder="Last name"><br>
<input type="submit" value="Submit">
</form>
http://www.w3schools.com/tags/att_input_placeholder.asp
I have the same problem, and I have add this code to my application and its work fine for me.
step -1 : added the jquery.placeholder.js plugin
step -2 :write the below code in your area.
$(function () {
$('input, textarea').placeholder();
});
And now I can see placeholders on the input boxes!
This is exactly what you want
$(document).tooltip({ selector: "[title]",
placement: "top",
trigger: "focus",
animation: false});
<form id="form">
<label for="myinput1">Browser tooltip appears on hover but disappears on clicking the input field. But this one persists while user is typing within the field</label>
<input id="myinput1" type="text" title="This tooltip persists" />
<input id="myinput2" type="text" title="This one also" />
</form>
[ref]
If you mean like a text in the background, I'd say you use a label with the input field and position it on the input using CSS, of course. With JS, you fade out the label when the input receives values and fade it in when the input is empty. In this way, it is not possible for the user to submit the description, whether by accident or intent.
If you don't insist on the hint being displayed inside the input field, a modern solution would use a label element with the for attribute referring to the id of the input field, like this:
<form action="input_password.htm">
<label for="username" title="This is your user name...">Username: </label><input id="username" name="Username" type="text" size="20" maxlength="20"></p>
</form>
If you click the label, the input field will get the input focus.
If you hover over the label, it will show a longer explanation.
Generally the label should describe well enough what the user has to enter (in the case of user name it should be very much obvious).
Define tooltip text
<input type="text" id="firstname" name="firstname" tooltipText="Type in your firstname in this box">
Initialize and configure the script
<script type="text/javascript">
var tooltipObj = new DHTMLgoodies_formTooltip();
tooltipObj.setTooltipPosition('right');
tooltipObj.setPageBgColor('#EEE');
tooltipObj.setCloseMessage('Exit');
tooltipObj.initFormFieldTooltip();
</script>