I'm using React and Yup to validate an email address.
My schema looks like this:
const registrationSchema = yup.object().shape({
email: yup.string().email().required()
})
And my code like this:
const handleOnSubmit = async () => {
const formData = {
email: props.emailInput
}
const isValid = await registrationSchema.isValid(formData)
setInputErrorState(isValid ? false : true)
}
The code above is doing the validation, but if I input a non-ASCII character like a Japanese or Chinese character it doesn't work. For example: ハロー#ハロー.com is passing the validation.
How can I allow only ASCII or romanji characters in my validation with YUP?
Ideal scenario:
attack.on.titan#gmail.com ✅
black#clover.com ✅
ハロー#ハロー.com ❌
Try this out using Regex
const isEmail = email => {
const re = /^(([^<>()\[\]\\.,;:\s#"]+(\.[^<>()\[\]\\.,;:\s#"]+)*)|(".+"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if (re.test(String(email).toLowerCase())) {
return email;
} else {
return false;
}
};
I removed yup and I used the answer suggested by Ibrahim shamma.
Here is the solution (RFC5322 compliant):
utils.tsx
// Regex taken from https://emailregex.com/
export const isEmail = email => {
const re = /^(([^<>()\[\]\\.,;:\s#"]+(\.[^<>()\[\]\\.,;:\s#"]+)*)|(".+"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if (re.test(String(email).toLowerCase())) {
return true;
}
return false;
};
Component/index.tsx
import React from 'react';
import {isEmail} from '../utils'
export interface ComponentInterface {
emailInput:string
setEmailInput: (e: any) => void
}
const Component : React.FC<ComponentInterface> = (props:ComponentInterface) => {
const [inputErrorState, setInputErrorState] = useState(false)
const handleOnChange = (e) => {
if (inputErrorState) {
setInputErrorState(false)
}
props.setEmailInput(e.target.value)
}
const handleOnSubmit = () => {
const isValid = isEmail(props.emailInput)
setInputErrorState(isValid ? false : true)
}
return ( <div>
<input
type='email'
id='props.emailInput'
value={emailInput}
onChange={handleOnChange}
></input>
{inputErrorState && (
<p>Your error message</p>
)}
</div> );
}
export default Component;
Related
I have a formik form with a select field; two options. When i use onClick I always get "yes" submitted and if i use onChange it does not work in that it does not allow me to choose anything, just always leaves the field the same as before.
I have read a ton of different things. I have tried the setFieldValue, and I have tried onBlur, I have tried numerous different ways in writing the onChange handler without any success. Maybe i am just not changing and writing it properly.
I have looked over the suggested questions already on here and for whatever reason i can not get them to work in my code.
import React, { useState, useRef } from 'react';
import { Form, Field, } from 'formik';
import emailjs from '#emailjs/browser';
const RsvpForm = ({ errors, touched, isValid, dirty }) => {
const form = useRef();
const sendEmail = (e) => {
e.preventDefault();
const userName = e.target[0].value;
const email = e.target[1].value;
const attending = state;
const plusOne = plusone;
const guests = e.target[4].value;
const guestNumber = e.target[5].value;
const guest_name = e.target[6].value;
const song = e.target[7].value;
const message = e.target[8].value;
let templateParams = {
userName: userName,
email: email,
attending: attending,
plusOne: plusOne,
guests: guests,
guestNumber: guestNumber,
guest_name: guest_name,
song: song,
message: message,
};
emailjs.send(process.env.REACT_APP_SERVICE_ID, process.env.REACT_APP_TEMPLATE_ID, templateParams, process.env.REACT_APP_PUBLIC_KEY)
.then((result) => {
console.log(result.text);
e.target.reset();
}, (error) => {
console.log(error.text);
});
};
const[state, attendingState] = useState("");
const onClick = (e) => {
let{value} = e.target;
if(value=== 'yes') {
attendingState('yes')
}else {
attendingState('no')
}
}
const[plusone, plusOnestate] = useState("");
const onPick = (e) => {
let{value} = e.target;
if(value=== 'no') {
plusOnestate('no')
}else {
plusOnestate('yes')
}
}
return (
<div className='form-group'>
<label className='col-form-label'>Plus One:</label>
<Field
component='select'
className={
touched.plusOne
? `form-control ${errors.plusOne ? 'invalid' : 'valid'}`
: `form-control`
}
name='plusOne'
type='select'
// onChange={(e) => setFieldValue('plusOne', e.target.value)}
onClick={onPick}
>
<option value="">Please select an answer</option>
<option value="yes">Yes, please add a plus one or few</option>
<option value="no">Just me!</option>
</Field>
{touched.plusOne && errors.plusOne && (
<small className='text-warning'><strong>{errors.plusOne}</strong></small>
)}
</div>
I am pretty new to React, and the only CRUD functionality I have done has been in MVC.net, so I am pretty lost here and none of the tutorials I am finding seem to be the situation I have going on...or maybe I am just misunderstanding them. The tips I got with my initial question helped, but I am still no getting it to work, so hopefully now I have supplies enough info to help other help me. I did no include all of the input fields because their are like 15 and it was just redundant.
I am pulling up the modal using this onClick event:
onClick={()=> {handleEditModal(item)}}
modalFunctions.js
// Modal Functionality
export function ModalFunctions() {
const [selectedRecord, setSelectedRecord] = useState([]);
const [openModal, setOpenModal] = useState(false);
const handleEditModal = item =>{
setOpenModal(true)
setSelectedRecord(item)
}
return {
selectedRecord,
setSelectedRecord,
openModal,
setOpenModal,
handleEditModal,
handleDetailModal
}
}
// Form Functionality
export function FormFunctions(validateOnChange = false, validate) {
const [values, setValues] = useState('');
const [errors, setErrors] = useState({});
const handleInputChange = e => {
const { name, value } = e.target
setValues({
...values,
[name]: value
})
if (validateOnChange)
validate({ [name]: value })
}
return {errors, handleInputChange, setErrors, setValues, values}
}
DataTable.js
// Imported Modal Functions
const {
selectedRecord,
openModal,
setOpenModal,
handleEditModal,
handleDetailModal
} = ModalFunctions();
const baseURL = 'http://localhost:8080/api/tanks';
// Fetch Data
useEffect(() => {
const fetchData = async () =>{
setLoading(true);
try {
const {data: response} = await axios.get(baseURL);
setTableData(response);
} catch (error) {
console.error(error.message);
}
setLoading(false);
};
fetchData();
}, [baseURL, setTableData, setLoading]);
// The Modal
return(
<Modal
title={ "Editing: " + (selectedRecord.tankName) }
openModal={openModal}
setOpenModal={setOpenModal}
>
<TankEdit
selectedRecord={selectedRecord}
setOpenModal={setOpenModal}
openModal={openModal}
/>
</Modal>
)
TankEdit.js
export function TankEdit(props) {
const { baseURL, openModal, selectedRecord, setOpenModal, setTableData } = props;
const validate = (fieldValues = item) => {
let temp = { ...errors }
if ('tankName' in fieldValues)
temp.tankName = fieldValues.tankName ? "" : "This field is required."
setErrors({
...temp
})
if (fieldValues === values)
return Object.values(temp).every(x => x === " ")
}
const {
values,
setValues,
errors,
setErrors,
handleInputChange,
} = FormFunctions(true, validate);
useEffect(() => {
if (selectedRecord !== null)
setValues({
...selectedRecord
})
}, [selectedRecord, setValues])
function editRecord() {
axios.put(`${baseURL}`, {
title: "Success",
body: "The record had been successfully updated"
}).then ((response) => {setTableData(response.data);})
}
const handleSubmit = e => {
e.preventDefault()
if (validate()) {
editRecord(values);
}
setOpenModal(false)
}
const item = values; // used for easier referencing (matches table item)
return (
<Form onSubmit={handleSubmit} open={openModal}>
<Grid>
<Controls.Input
name="tankName"
label="Tank Name"
value={item.tankName}
onChange={handleInputChange}
error={errors.tankName}
/>
</Grid>
<Grid>
<Controls.Button
type="submit"
text="Submit"
/>
<Controls.Button
text="Cancel"
color="default"
onClick={()=>{setOpenModal(false)}}
/>
</Grid>
</Form>
)
}
Input.js
export default function Input(props) {
const { error=null, label, name, onChange, value, ...other } = props;
return (
<TextField
variant="outlined"
label={label}
name={name}
value={value}
defaultValue=''
onChange={onChange}
{...other}
{...(error && {error:true,helperText:error})}
/>
)
}
My company is only wanting a Read and an Update function, since Creating and Deletion will be handled another ways, so this is my final hangup. I think I am close, but I am missing something.
Can anyone point me in the right direction?
THANKS!!!!
If you want to write an update request you would use axios.put to send the data to your back-end.
In your handleSubmit function you do:
let response = await axios.put('http://-------/api/tanks', { name: 'tank' });
(The second parameter is an object that needs to contain all the form data fields)
Also make sure you call e.preventDefault() in the handleSubmit function so you don't accidentally navigate elsewhere.
Then you will update your database or whatever using your back-end.
for update you should use put or patch method
and you should send id of item you want to update in request url.
I insert 2 example here.
this is for put:
const res = await axios.put('/api/article/123', {
title: 'Making PUT Requests with Axios',
status: 'published'
});
this is for patch:
const res = await axios.patch('/api/article/123', {
title: 'Making PUT Requests with Axios',
status: 'published'
});
I made code and if login information of my code matches well or not, it shows alert window. This worked well. But I want to make it move to new page when login matches well by using Link. So I changed the code when login information matches well. But I can't move the page when I press the button. I think the way to use Link is wrong. Can you guys teach me how to fix it? Thank you.
I skipped useless codes and styled-components
//login information
const User = {
email : 'monta#naver.com',
pw : '1234'
}
function LoginForm() {
const [email, setEmail] = useState ('');
const [pw, setPw] = useState('');
const handleEmail = (e) => {
setEmail(e.target.value);
}
const handlePw = (e) => {
setPw(e.target.value);
}
const onClickConfirmButton = (e) => {
// when information matches well. I think this part has an error
if(email === User.email && pw === User.pw) {
<Link to="/lecture" style={{textDecoration : 'none'}} />
}
//when information is wrong
else {
alert('등록되지 않은 회원입니다.')
}
}
return (
<div>
<AuthFormBlock>
<StyledInput
placeholder='이메일'
value={email}
onChange={handleEmail}>
</StyledInput>
<StyledInput
placeholder='비밀번호'
type="password"
value={pw}
onChange={handlePw}>
</StyledInput>
<LoginButton
onClick={onClickConfirmButton}>
로그인
</LoginButton>
</AuthFormBlock>
</div>
)
}
export default LoginForm;
Link component is used like anchor tag of html so if you want to change the page programmatically you can use 'useNavigation' hook :
function LoginForm() {
const [email, setEmail] = useState ('');
const [pw, setPw] = useState('');
const navigate = useNavigate();
const handleEmail = (e) => {
setEmail(e.target.value);
}
const handlePw = (e) => {
setPw(e.target.value);
}
const onClickConfirmButton = (e) => {
// when information matches well. I think this part has an error
if(email === User.email && pw === User.pw) {
navigate('/lecture');
}
//when information is wrong
else {
...
}
}
I'm new to React building a simple app and I get this error when I'm trying to add a comment to a photo, the code is working and the state is changing correctly when I hit enter
I the error in this line
const photo = photos.find((item) => item.id === Number(photo_id));
the photos are defined and the id is defined but I get the photo is undefined
I really appreciate it if anyone could help
here's the code
import { useNavigate, useParams } from 'react-router-dom';
import { useSelector, connect } from 'react-redux'
import Photo from './Photo';
import { useState } from 'react';
import { addComment } from './actions'
const PhotoDetail = ({ addComment }) => {
const {photo_id} = useParams();
const navigate = useNavigate();
const [text, setText] = useState('');
const photos = useSelector((state) => state.photoList.photos);
const photo = photos.find((item) => item.id === Number(photo_id));
console.log('here photo id', photo_id)
console.log('here photo', photo)
console.log('here photos', photos)
const comments = photo['comment'];
const handelKeyDown = (e) => {
if (e.key === "Enter") {
const commentData = {text, photo_id}
addComment(commentData);
// navigate('/' + photo.id);
}
}
return (
<div className="detail">
<div className="photos photoDetail">
<Photo key={photo.id} photo={photo}/>
</div>
<div>
<h2>Comments</h2>
<div>
{ comments.map((comment) => (
<p key={comment.id}>{comment.text}</p>
)) }
</div>
<input type="text" value={text} onChange = {
(e) => setText(e.target.value)
} onKeyDown={
handelKeyDown
}/>
</div>
</div>
);
}
const mapDispatchToProps = dispatch => ({
addComment: commentData => dispatch(addComment(commentData))
})
export default connect(null, mapDispatchToProps) (PhotoDetail);
here's the action
export const addComment = (commentData) => {
console.log('test')
return {
type:"ADDCOMMENT",
payload: commentData
};
};
and here's the Reducer
case "ADDCOMMENT":
const idx = Math.floor(Math.random() * 10000) + 1;
const { text, photo_id } = action.payload;
const newComment = {idx, text}
return { ...state, photos:[state.photos.map((image) =>
image.id === photo_id ? image.comment.push(newComment) && image : image),] }
the console
the console
find will return undefined in case nothing matches the required condition.
So, looks like item.id === Number(photo_id) is probably not resolving to true for any photo in photos.
Then, you are trying to access comment on undefined, that's why there's a TypeError.
In action payload photo_id has string type and in reducer you have added === check but image.id holds number type.
Added, a comment in below code for your better understanding.
case "ADDCOMMENT":
const idx = Math.floor(Math.random() * 10000) + 1;
const { text, photo_id } = action.payload;
const newComment = {idx, text}
return { ...state, photos:[state.photos.map((image) =>
// HERE below condition fails always bcz of strict type check, to photo_id
//- either add Number convertion or in payload itself send string type
image.id === photo_id ? image.comment.push(newComment) && image : image),] }
thank you guys your answers were very helpful but I change the logic
I already have the photo I didn't need to map throw the photos so I just add the comment to the photo and return the state and it works!
case "ADDCOMMENT":
const idx = Math.floor(Math.random() * 10000) + 1;
const { text, photo } = action.payload;
console.log('the photo from reducer', photo)
const newComment = {idx, text};
photo.comment.push(newComment) // adding the comment to the photo
// return { ...state, photos:[state.photos.map((image) =>
// Number(image.id) === Number(photo.id) ? image.comment.push(newComment) && image : image),] }
return state;
I'm building an app using react, redux, and redux-saga.
The situation is that I'm getting information from an API. In this case, I'm getting the information about a movie, and I will update this information using a basic form.
What I would like to have in my text fields is the value from the object of the movie that I'm calling form the DB.
This is a brief part of my code:
Im using 'name' as an example.
Parent component:
const MovieForm = (props) => {
const {
movie,
} = props;
const [name, setName] = useState('');
const handleSubmit = (e) => {
e.preventDefault();
onSubmit({
name,
});
};
const handleSetValues = () => {
console.log('hi');
console.log(movie, name);
setName(movie.name);
setValues(true);
};
useEffect(() => {
if (movie && values === false) {
handleSetValues();
}
});
return (
<Container>
<TextField
required
**defaultValue={() => {
console.log(movie, name);
return movie ? movie.name : name;
}}**
label='Movie Title'
onChange={(e) => setName(e.target.value)}
/>
</Container>
);
};
export default MovieForm;
....
child component
const MovieUpdate = (props) => {
const { history } = props;
const { id } = props.match.params;
const dispatch = useDispatch();
const loading = useSelector((state) => _.get(state, 'MovieUpdate.loading'));
const created = useSelector((state) => _.get(state, 'MovieUpdate.created'));
const loadingFetch = useSelector((state) =>
_.get(state, 'MovieById.loading')
);
const movie = useSelector((state) => _.get(state, 'MovieById.results'));
useEffect(() => {
if (loading === false && created === true) {
dispatch({
type: MOVIE_UPDATE_RESET,
});
}
if (loadingFetch === false && movie === null) {
dispatch({
type: MOVIE_GET_BY_ID_STARTED,
payload: id,
});
}
});
const updateMovie = (_movie) => {
const _id = id;
const obj = {
id: _id,
name: _movie.name,
}
console.log(obj);
dispatch({
type: MOVIE_UPDATE_STARTED,
payload: obj,
});
};
return (
<div>
<MovieForm
title='Update a movie'
buttonTitle='update'
movie={movie}
onCancel={() => history.push('/app/movies/list')}
onSubmit={updateMovie}
/>
</div>
);
};
export default MovieUpdate;
Then, the actual problem is that when I use the default prop on the text field the information appears without any problem, but if i use defaultValue it is empty.
Ok, I kind of got the answer, I read somewhere that the defaultValue can't be used int the rendering.
So I cheat in a way, I set the properties multiline and row={1} (according material-ui documentation) and I was able to edit this field an receive a value to display it in the textfield