I've 2 controllable text input with validation:
required={true}
minLength={5}
maxLength={10}
Both are share the same state, so their text are always in sync.
value={text}
onChange={(e) => setText(e.target.value)}
The initial value of them are 'ab':
const [text, setText] = useState("ab");
Then I added a css for visualizing the validation state:
input[type="text"]:valid {
background-color: lightgreen !important;
}
input[type="text"]:invalid {
background-color: pink !important;
}
The problem:
At the first load, both color are green.
The value are 'ab' it doesn't satisfy the validation.
Why not colored red?
When i added one character of the first textbox => abc
The color is red, that's to be expected.
But the second one still colored green. That's not expected.
Anyone can fix the problem? I want the text & validation are in sync.
Sandbox: enter link description here
code:
import "./styles.css";
import { useState } from "react";
import "./App.css";
export default function App() {
const [text, setText] = useState("ab");
return (
<div className="App">
<input
type="text"
required={true}
minLength={5}
maxLength={10}
value={text}
onChange={(e) => setText(e.target.value)}
/>
<br />
<input
type="text"
required={true}
minLength={5}
maxLength={10}
value={text}
onChange={(e) => setText(e.target.value)}
/>
</div>
);
}
css:
input[type="text"]:valid {
background-color: lightgreen !important;
}
input[type="text"]:invalid {
background-color: pink !important;
}
To be completely honest, I don't know why React is behaving this way, but an alternative would be to add a 'valid' or 'invalid' class to the input depending on the 'text' value:
<input
className={text.length >= 5 && text.length <= 10 ? 'valid' : 'invalid'}
type="text"
required={true}
value={text}
onChange={(e) => setText(e.target.value)}
/>
.valid {
background-color: lightgreen !important;
}
.invalid {
background-color: pink !important;
}
<fieldset class="crm-public-form-item crm-group payment_options-group" style="display: block !important; visibility: visible;">
<div class="crm-public-form-item crm-section payment_processor-section" style="display: block;">
<div class="content">
<input class="payment_processor_1 crm-form-radio" value="5" type="radio" id="CIVICRM_QFID_5_payment_processor_id" name="payment_processor_id">
<input class="payment_processor_2 crm-form-radio" value="3" type="radio" id="CIVICRM_QFID_3_payment_processor_id" name="payment_processor_id">
</div>
</div>
</fieldset>
<div id="crm-submit-buttons" class="crm-submit-buttons" style="visibility: visible; display: block;">
<button class="crm-form-submit default validate crm-button crm-button-type-upload crm-button_qf_Main_upload" value="1" type="submit" name="_qf_Main_upload" id="_qf_Main_upload-bottom">…</button>
</div>
I have this html block and I am bit stuck with validation. Conditions are
payment_options-group fieldset must present on this page
And at least one of the radio button is checked
Then show crm-submit-buttons div
Otherwise hide crm-submit-buttons div
I have tried the following but could not get it to work, also not sure how to define the condition payment_options-group fieldset must present on this page. This is important because "crm-submit-buttons" is controlled by other fieldset, which is not present on this page.
if( $("#payment_options-group").val().length === 1 ) {
$("div#crm-submit-buttons").show();
}
else {
$("div#crm-submit-buttons").hide();
}
Any help would be appreciated. Thanks in advance.
You can check if the length is > 0 then checkbox is checked and fieldsets is present depending on this show or hide div .
Demo Code :
console.log($(".payment_options-group").length + "--length of fielset")
console.log($("input[name=payment_processor_id]:checked").length + "--length of chekd")
//check if fieldset is prsent and checked length if > 0
if (($(".payment_options-group").length > 0) && ($("input[name=payment_processor_id]:checked").length > 0)) {
$("div#crm-submit-buttons").show();
} else {
$("div#crm-submit-buttons").hide();
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<fieldset class="crm-public-form-item crm-group payment_options-group" style="display:block !important; visibility: visible;">
<div class="crm-public-form-item crm-section payment_processor-section" style="display:block;">
<div class="content">
<input class="payment_processor_1 crm-form-radio" value="5" type="radio" id="CIVICRM_QFID_5_payment_processor_id" name="payment_processor_id">
<input class="payment_processor_2 crm-form-radio" value="3" type="radio" id="CIVICRM_QFID_3_payment_processor_id" name="payment_processor_id" checked>
</div>
</div>
</fieldset>
<div id="crm-submit-buttons" class="crm-submit-buttons" style="visibility:visible; display:block;">
<button class="crm-form-submit default validate crm-button crm-button-type-upload crm-button_qf_Main_upload" value="1" type="submit" name="_qf_Main_upload" id="_qf_Main_upload-bottom">…</button>
</div>
Here is a demo with checkboxes that might be easier to understand as you can uncheck to see the button disappear again:
$(".crm-submit-buttons").hide(); // hides button on load
$('.crm-form-radio').on('click', function() {
if ($('.payment_options-group').length > 0 && $('.crm-form-radio:checked').length) {
$(".crm-submit-buttons").show();
} else {
$(".crm-submit-buttons").hide();
}
})
fieldset{
width: 200px;
}
button {
margin-top: 10px;
border-radius: 4px;
background: lightblue;
display: inline-block;
padding: 5px 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<fieldset class="payment_options-group">
<input class="crm-form-radio" value="5" type="checkbox" name="payment_processor_id">
<input class="crm-form-radio" value="3" type="checkbox" name="payment_processor_id">
</fieldset>
<div class="crm-submit-buttons"><button>Button</button></div>
I am trying to add checkboxes to a login page using code that I found at react-bootstrap.github.io
The problem I'm having is the checkbox is overlapping the label.
My code (register.js) is below
import React, { useState } from 'react'
import { Link } from 'react-router-dom';
import { request } from './services/Request';
import { Button, FormGroup, FormControl, FormLabel, FormCheck } from 'react-bootstrap';
//import { FormRow } from 'react-bootstrap/Form';
import "./Styles/register.css"
export const Register = () => {
return (
<div style={{display: "flex", justifyContent: "center"}}>
<h1> Register </h1>
<div className="Register">
<form onSubmit={handleSubmit}>
</FormGroup>
<FormGroup controlId="email" bsSize="large">
<FormLabel>Email</FormLabel>
<FormControl
required
autoFocus
type="email"
//value={email}
onChange={ onChangeHandlerFn }
/>
</FormGroup>
<FormGroup controlId="password" bsSize="large">
<FormLabel>Password</FormLabel>
<FormControl
required
autoFocus
type="password"
//value={password}
onChange={ onChangeHandlerFn }
/>
</FormGroup>
<FormGroup controlId="password" bsSize="large">
{['checkbox'].map((type) => (
<div key={`default-${type}`} className="mb-3">
<FormCheck
type={type}
id={`default-${type}`}
label={`I am an individual`}
/>
</div>
))}
<Button variant="primary" type="submit" block>Register</Button>
<span>
Have an account?
Sign in
</span>
<ul>
<li><Link to="/privacy">Privacy & Terms</Link></li>
</ul>
</form>
</div>
</div>
);
};
The problem is the checkboxes are overlapping their labels:
Here is my css:
#media all and (min-width: 480px) {
.Register {
padding: 60px 0;
}
.Register form {
margin: 0 auto;
max-width: 320px;
}
}
ul {
list-style-type: none !important;
margin: 0;
padding: 0;
overflow: hidden;
}
Everything works, for the sake of length, I've done post all of the code.
Any help would be greatly appreciated.
What about adding to the left padding of the label so it clears the checkbox?
label {
padding-left:15px;
}
I have the following login form:
<!-- login box -->
<form action="php/login.php" method="POST" id="signin">
<span id="oldLoginProblem" style="display: inline-block;" ><h3>Login</h3></span>
//appears when error
<span id="newLoginProblem" style="display: inline-block; color: red; font-weight: bold;"><h3>Invalid Login · <a href='recover'>Forgot Password?</a></h3></span>
<table id="loginTable">
<tr><td>Email</td><td><input type="text" id="email" name="email" title="Enter your email"/></td></tr>
<tr><td>Pass</td><td><input type="password" id="password" name="password" title="Enter your password"/>
<?php
if(isset($_SERVER['HTTP_USER_AGENT']))
{
$agent = $_SERVER['HTTP_USER_AGENT'];
}
if(!(preg_match("/firefox/si", $agent)))
{
//workaround for a jQuery plugin I'm using
echo "<input type='text'/>";
}
?>
</td></tr>
</table>
<input type="button" value="Login" id="loginSub"/>
<input type="button" value="Sign Up" onClick="document.location.href='register'"/>
</form>
I can move the "Sign Up" button outside of the form, obviously, if needed. I'm just trying to make the form submit when Enter is pressed. Right now, it does no such thing. The button isn't a type=submit either, so that may be the case.
Any help? I'm using type=button because I'm utilizing jQuery UI buttons.
Edit:
Here's the code that handles my login submission. Perhaps I can make Enter run this code?
$('#loginSub').live('click', function() {
$.post("php/login.php", $("#signin").serialize(), function(data) {
if (data == "Invalid") {
$('#oldLoginProblem').fadeOut('fast', function() {
$('#newLoginProblem').fadeOut('fast', function() {
$('#newLoginProblem').fadeIn('fast');
});
});
}
else {
window.location.replace("home");
}
});
});
$('#input_text').keyup(function(e) {
if(e.keyCode == 13) {
$("#signin").submit();
}
});
$('#input_text').keyup(function(e) {
if(e.keyCode == 13) {
document.singin.submit();
}
});
How do I make a textbox that has a grayed out content, and when I click on it to enter text, the grayed out portion, it disappears and allows me to enter the desired text?
Example:
A "First Name" text box. The words "First Name" are inside the text box grayed out, when I click, those words disappear and I write my name in it.
Chrome, Firefox, IE10 and Safari support the html5 placeholder attribute
<input type="text" placeholder="First Name:" />
In order to get a more cross browser solution you'll need to use some javascript, there are plenty of pre-made solutions out there, though I don't know any off the top of my head.
http://www.w3schools.com/tags/att_input_placeholder.asp
This answer illustrates a pre-HTML5 approach. Please take a look at Psytronic's answer for a modern solution using the placeholder attribute.
HTML:
<input type="text" name="firstname" title="First Name" style="color:#888;"
value="First Name" onfocus="inputFocus(this)" onblur="inputBlur(this)" />
JavaScript:
function inputFocus(i) {
if (i.value == i.defaultValue) { i.value = ""; i.style.color = "#000"; }
}
function inputBlur(i) {
if (i.value == "") { i.value = i.defaultValue; i.style.color = "#888"; }
}
With HTML5, you can do this natively with: <input name="first_name" placeholder="First Name">
This is not supported with all browsers though (IE)
This may work:
<input type="first_name" value="First Name" onfocus="this.value==this.defaultValue?this.value='':null">
Otherwise, if you are using jQuery, you can use .focus and .css to change the color.
If you're targeting HTML5 only you can use:
<input type="text" id="firstname" placeholder="First Name:" />
For non HTML5 browsers, I would build upon Floern's answer by using jQuery and make the javascript non-obtrusive. I would also use a class to define the blurred properties.
$(document).ready(function () {
//Set the initial blur (unless its highlighted by default)
inputBlur($('#Comments'));
$('#Comments').blur(function () {
inputBlur(this);
});
$('#Comments').focus(function () {
inputFocus(this);
});
})
Functions:
function inputFocus(i) {
if (i.value == i.defaultValue) {
i.value = "";
$(i).removeClass("blurredDefaultText");
}
}
function inputBlur(i) {
if (i.value == "" || i.value == i.defaultValue) {
i.value = i.defaultValue;
$(i).addClass("blurredDefaultText");
}
}
CSS:
.blurredDefaultText {
color:#888 !important;
}
The shortest way is to directly add the below code as additional attributes in the input type that you want to change.
onfocus="if(this.value=='Search')this.value=''"
onblur="if(this.value=='')this.value='Search'"
Please note: Change the text "Search" to "go" or any other text to suit your requirements.
This works:
<input type="text" id="firstname" placeholder="First Name" />
Note: You can change the placeholder, id and type value to "email" or whatever suits your need.
More details by W3Schools at:http://www.w3schools.com/tags/att_input_placeholder.asp
But by far the best solutions are by Floern and Vivek Mhatre ( edited by j0k )
I have a code snippet below, that is a typical web page.
.Submit {
background-color: #008CBA;
border: 3px;
color: white;
padding: 8px 26px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
}
div {
background-color: yellow;
}
<div>
<center>
<p>Some functions may not work, such as the css ect.
<p>First Name:<input type="text" id="firstname" placeholder="John" />
<p>Surname:<input type="text" id="surname" placeholder="Doe" />
<p>Email:<input type="email" id="email" placeholder="john.doe#example.com" />
<p>Password:<input type="email" id="email" placeholder="john.doe#example.com" />
<br /><button class="submit">Submit</button> </center>
</div>
This is an elaborate version, to help you understand
function setVolatileBehavior(elem, onColor, offColor, promptText){ //changed spelling of function name to be the same as name used at invocation below
elem.addEventListener("change", function(){
if (document.activeElement == elem && elem.value==promptText){
elem.value='';
elem.style.color = onColor;
}
else if (elem.value==''){
elem.value=promptText;
elem.style.color = offColor;
}
});
elem.addEventListener("blur", function(){
if (document.activeElement == elem && elem.value==promptText){
elem.value='';
elem.style.color = onColor;
}
else if (elem.value==''){
elem.value=promptText;
elem.style.color = offColor;
}
});
elem.addEventListener("focus", function(){
if (document.activeElement == elem && elem.value==promptText){
elem.value='';
elem.style.color = onColor;
}
else if (elem.value==''){
elem.value=promptText;
elem.style.color = offColor;
}
});
elem.value=promptText;
elem.style.color=offColor;
}
Use like this:
setVolatileBehavior(document.getElementById('yourElementID'),'black','gray','Name');
You can use Floern's solution. You may also want to disable the input while you set the color to gray. http://www.w3schools.com/tags/att_input_disabled.asp
Here's a one-liner slim way for layering text on top of an input in jQuery using ES6 syntax.
$('.input-group > input').focus(e => $(e.currentTarget).parent().find('.placeholder').hide()).blur(e => { if (!$(e.currentTarget).val()) $(e.currentTarget).parent().find('.placeholder').show(); });
* {
font-family: sans-serif;
}
.input-group {
position: relative;
}
.input-group > input {
width: 150px;
padding: 10px 0px 10px 25px;
}
.input-group > .placeholder {
position: absolute;
top: 50%;
left: 25px;
transform: translateY(-50%);
color: #929292;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="input-group">
<span class="placeholder">Username</span>
<input>
</div>